forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
89 lines (77 loc) · 2.01 KB
/
H.cpp
File metadata and controls
89 lines (77 loc) · 2.01 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
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <complex>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>
#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 << " = "; cout << (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; }
using namespace std;
const int MN = 200111;
int n, a[MN];
bool visited[MN];
int cur[MN], nCur;
int res[MN], go[MN];
void solve() {
FOR(i,1,n) if (a[i] == i) {
cout << -1 << endl;
return ;
}
memset(res, 0, sizeof res);
memset(visited, false, sizeof visited);
FOR(i,1,n) if (!visited[i]) {
nCur = 1; cur[1] = i;
int u = a[i];
while (u != i) {
cur[++nCur] = u;
u = a[u];
}
FOR(i,1,nCur) visited[cur[i]] = true;
// PR(cur, nCur);
FOR(t,1,nCur) {
int u = cur[t];
if (a[u] > u) {
res[u*2] = u*2;
go[u] = u*2-1;
} else {
res[u*2-1] = u*2-1;
go[u] = u*2;
}
}
FOR(t,1,nCur) {
int u = cur[t];
res[go[u]] = go[a[u]];
}
}
FOR(i,1,n+n) cout << res[i] << ' '; cout << endl;
}
int main() {
ios :: sync_with_stdio(false);
freopen("restore.in", "r", stdin);
freopen("restore.out", "w", stdout);
while (cin >> n) {
FOR(i,1,n) cin >> a[i];
solve();
}
return 0;
}