QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#323887 | #8130. Yet Another Balanced Coloring Problem | ucup-team055# | RE | 1ms | 3752kb | C++17 | 1.4kb | 2024-02-10 14:00:36 | 2024-02-10 14:00:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define all(p) p.begin(),p.end()
using ll =long long;
struct uf{
int n;
vector<int> par;
uf(int n_):n(n_){
par.resize(n);
rep(i,0,n) par[i]=i;
}
int root(int a){
if(a==par[a]) return a;
par[a]=root(par[a]);
return par[a];
}
int unite(int a,int b){
a=root(a),b=root(b);
if(a==b) return 0;
if(a>b) swap(a,b);
par[b]=a;
return 1;
}
};
void solve(){
int N,M;
cin>>N>>M;
vector<int> p(N-1),q(M-1);
rep(i,0,N-1) cin>>p[i],p[i]--;
rep(i,0,M-1) cin>>q[i],q[i]--;
int K=p[0];
uf T(K*2);
auto f=[&](ll n,vector<int> v) -> void {
vector<int> tmp(n,-1);
rep(i,0,K){
tmp[i]=i;
}
rep(i,0,n-1){
if(tmp[i]==-1) continue;
if(tmp[v[i]]==-1) tmp[v[i]]=tmp[i];
else{
int a=tmp[i],b=tmp[v[i]];
tmp[v[i]]=-1;
T.unite(2*a,2*b+1);
T.unite(2*a+1,2*b);
}
}
};
f(N,p),f(M,q);
rep(i,0,K) cout<<(T.root(i)&1?'R':'B');
cout<<"\n";
}
int main(){
int T;
cin>>T;
while(T--) solve();
}
/*
2
7 7
5 5 6 6 7 7
5 6 5 6 7 7
5 4
4 4 5 5
4 4 4
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3752kb
input:
2 7 7 5 5 6 6 7 7 5 6 5 6 7 7 5 4 4 4 5 5 4 4 4
output:
BRRB BRR
result:
ok ok (2 test cases)
Test #2:
score: -100
Runtime Error
input:
10000 6 6 5 5 5 5 6 5 6 6 6 6 9 6 7 9 7 7 6 8 9 9 6 6 6 6 6 9 8 9 9 8 7 7 8 8 9 7 7 8 7 7 7 8 6 10 4 5 5 6 6 4 6 5 7 8 9 8 9 10 6 9 6 6 6 6 6 6 9 7 8 8 9 9 9 8 9 8 6 6 7 6 7 8 7 9 7 6 7 8 9 9 9 8 6 7 7 5 8 8 8 9 7 5 6 5 8 7 8 8 7 6 6 5 5 6 7 8 5 7 6 6 7 7 9 9 8 9 8 8 8 8 9 9 9 9 8 8 9 9 8 9 9 8 8 8 ...
output:
BRRB BRBRRB BRRBBRBR BRB BRRBB BRBRBRB BRBRR BRRBB BRRBRBB BRRBBRR BRR BRRBBRB BRBRB BRRBBRR BRBRBRR BRRBB BRBRR BRRBB BRBR BRRBB BRRBBRRB BRRBB BRRBB BRBR BRRBB BRBR BRRBBR BRBRRBB BRRB BRBRBRR BRRBRB BRRBB BRRBBR BRRBBR BRBRRBR BRR BRBRR BRBRBR BRBRBR BRRBB BRRBR BRBRRBB BRRB BRRBRB BRBR BRRBBR BR...