#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 1001000
#define pp pair<int, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
using namespace std;
int n, p[N], pos[N], mark[N];
bool Ok(int crazy) {
FOR(i, 1, n) mark[i] = 0;
mark[p[crazy]] = 1;
FOR(i, 1, n) if (i != crazy) {
if (mark[i] == 0) {
if (p[i] != i) return false;
mark[i] = 1;
}
else {
if (p[i] == i || mark[p[i]] == 1) return false;
mark[p[i]] = 1;
}
}
return true;
}
void Solve() {
cin >> n;
FOR(i, 1, n) cin >> pos[i];
FOR(i, 1, n) p[pos[i]] = i;
FOR(i, 1, n) if (i != p[i]) {
int cnt = 0;
if (Ok(i)) cnt++;
if (Ok(pos[i])) cnt++;
if (cnt == 1) cout << i << '\n';
else cout << 0 << '\n';
return;
}
cout << 0 << '\n';
}
int main() {
#ifdef NERO
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
double stime = clock();
#else
//freopen(taskname".inp","r",stdin);
//freopen(taskname".out","w",stdout);
#endif //NERO
IO;
int test;
cin >> test;
while (test--) Solve();
#ifdef NERO
double etime = clock();
cerr << "Execution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
#endif // NERO
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |