forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
98 lines (84 loc) · 2.56 KB
/
E.cpp
File metadata and controls
98 lines (84 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 1000111;
struct Cycle {
vector<int> x;
};
bool operator < (const Cycle& a, const Cycle& b) {
return SZ(a.x) < SZ(b.x);
}
int n, a[MN], res[MN];
bool visited[MN];
int main() {
while (scanf("%d", &n) == 1) {
FOR(i,1,n) scanf("%d", &a[i]);
vector<Cycle> even;
memset(visited, 0, sizeof visited);
FOR(i,1,n) if (!visited[i]) {
int u = i;
visited[u] = true;
Cycle cur;
cur.x.clear();
cur.x.push_back(u);
while (!visited[a[u]]) {
u = a[u];
cur.x.push_back(u);
visited[u] = true;
}
// PR0(cur.x, SZ(cur.x));
if (SZ(cur.x) % 2) {
// odd cycle: expand it to res
vector<int> t;
t.resize(SZ(cur.x));
int v = 0;
REP(u,SZ(cur.x)) {
t[v] = cur.x[u];
v = (v + 2) % SZ(cur.x);
}
REP(u,SZ(t)) {
int v = (u + 1) % SZ(t);
res[t[u]] = t[v];
}
}
else {
even.push_back(cur);
}
}
if (SZ(even) % 2) {
cout << -1 << endl;
continue;
}
sort(even.begin(), even.end());
bool ok = true;
for(int i = 0; i < SZ(even); i += 2) {
if (SZ(even[i].x) != SZ(even[i+1].x)) {
ok = false;
break;
}
vector<int> t;
REP(j,SZ(even[i].x)) {
t.push_back(even[i].x[j]);
t.push_back(even[i+1].x[j]);
}
REP(u,SZ(t)) {
int v = (u + 1) % SZ(t);
res[t[u]] = t[v];
}
}
if (!ok) {
cout << -1 << endl;
continue;
}
FOR(i,1,n) printf("%d ", res[i]);
puts("");
}
}