# Yukicoder No.100 直列あみだくじ

,

http://yukicoder.me/problems/no/100

これはそこそこ落ちにくいのでは。

## implementation

#include <iostream>
#include <vector>
#include <functional>
#include <sys/signal.h>
#include <sys/time.h>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using namespace std;
void handler(int) {
cout << "No" << endl;
exit(EXIT_SUCCESS);
}
int main() {
// timer
signal(SIGALRM, handler);
itimerval tv;
tv.it_value.tv_sec = 4;
tv.it_value.tv_usec = 800 * 1000;
tv.it_interval = {};
setitimer(ITIMER_REAL, &tv, NULL);
// solve
int n; cin >> n;
vector<int> f(n); repeat (i,n) { cin >> f[i]; -- f[i]; }
vector<int> g(n, -1), g_inv(n, -1);
function<bool (int, int)> def = [&](int i, int j) {
if (g    [i] != -1 and g    [i] != j) return false;
if (g_inv[j] != -1 and g_inv[j] != i) return false;
if (g[i] == j and g_inv[j] == i) return true;
g[i] = j;
g_inv[j] = i;
return def(j, f[i]);
};
function<bool (int)> go = [&](int i) {
while (i < n and g[i] != -1) ++ i;
if (i == n) return true;
repeat (j,n) if (g_inv[j] == -1) {
vector<int> h = g;
vector<int> h_inv = g_inv;
if (def(i, j)) {
if (go(i+1)) return true;
}
g = h;
g_inv = h_inv;
}
return false;
};
cout << (go(0) ? "Yes" : "No") << endl;
return 0;
}