-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path015-marbles.cpp
More file actions
63 lines (55 loc) · 1.31 KB
/
Copy path015-marbles.cpp
File metadata and controls
63 lines (55 loc) · 1.31 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
/*
* Marbles [106251C]
* Problem: Gym/ICPC contest 106251 - problem C
* Verdict: ACCEPTED Solved: 2026-06-20
* Language: C++23 (GCC 14-64, msys2)
* Runtime: 156 ms Memory: 1000 KB
* Tags: n/a
* Author: team "balls" (HossamIsmail, BidoTeima, a1abay)
* Source: https://codeforces.com/gym/106251/submission/379573531
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 3;
int vis[N], p[N];
void dfs(int node){
vis[node]++;
if(vis[p[node]]) vis[p[node]]++;
else dfs(p[node]);
}
bool vis2[N];
char color[N];
void dfs2(int node, char c){
// cout << node << c << '\n';
color[node] = c;
vis2[node] = 1;
if(!vis2[p[node]]){
dfs2(p[node], c == 'G'? 'B' : 'G');
}
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> p[i],vis[i]=0,vis2[i]=0;
for(int i = 1; i <= n; i++){
if(!vis[i]){
dfs(i);
// if(vis[i] > 1){
color[i] = 'R';
vis2[i] = 1;
dfs2(p[i], 'G');
// }
// cout<<'\n';
}
cout<<color[i];
// assert(color[i]!=color[p[i]]);
}
cout<<'\n';
}
return 0;
}