#include #include #include #include #include #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 f(n); repeat (i,n) { cin >> f[i]; -- f[i]; } vector g(n, -1), g_inv(n, -1); function 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 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 h = g; vector 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; }