-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRestoreSequenceolution
More file actions
49 lines (41 loc) · 867 Bytes
/
RestoreSequenceolution
File metadata and controls
49 lines (41 loc) · 867 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
queue<int> primes;
int maxi = 4 * 1e6;
bool sieve [maxi + 1] {0};
for (int i = 2; i <= maxi; i++) {
if (!sieve[i]) {
primes.push(i);
int j = i + i;
while (j <= maxi) {
sieve[j] = 1;
j += i;
}
}
}
while (t-- > 0) {
int n, v, d;
cin >> n;
int a[n] {0};
for (int i = 0; i < n; i++) {
cin >> v;
v--;
if (a[v] == 0 && a[i] == 0) {
a[i] = a[v] = primes.front();
primes.pop();
} else if (a[i] == 0) {
a[i] = a[v];
}// else if (a[v] == 0) {
a[v] = a[i];
//}
}
for (int i = 0; i < n; i++) {
cout << a[i] << ' ';
}
cout << endl;
}
return 0;
}