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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f3f3fll #define debug(x) cerr<<#x<<"="<<x<<endl using namespace std; using ll=long long; using ld=long double; using pli=pair<ll,int>; using pi=pair<int,int>; template<typename A> using vc=vector<A>; inline int read() { int s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline ll lread() { ll s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } template<const int N,const int M> struct graph { int head[N+5]; int ww[M+5]; int t[M+5]; int x[M+5]; int cntm; graph(){ cntm=1;} inline void clear(int n=N) { cntm=1; for(int i=1;i<=n;i++) head[i]=0; } inline void ad(int u,int v,int w=0) { cntm++; t[cntm]=v; x[cntm]=head[u]; ww[cntm]=w; head[u]=cntm; } inline void add(int u,int v,int w=0) { ad(u,v,w); ad(v,u,w); } inline int st(int num){ return head[num];} inline int to(int num){ return t[num];} inline int nx(int num){ return x[num];} inline int w(int num){ return ww[num];} }; graph<100000,300000>g; mt19937 _rand(0); bool v[50005]; vc<int>e[50001]; bool in[100001]; int sta[100001],top; int dfn[100001]; int low[100001]; int scc[100001]; int n,c,tim; inline void R() { for(int i=1;i<=n;i++) v[i]=_rand()&1; } void dfs(int num) { dfn[num]=low[num]=++tim; in[num]=1,sta[++top]=num; for(int i=g.st(num);i;i=g.nx(i)) { int p=g.to(i); if(!dfn[p]) dfs(p),low[num]=min(low[num],low[p]); else if(in[p]) low[num]=min(low[num],dfn[p]); } if(dfn[num]!=low[num]) return ; c++;int now; do { now=sta[top--]; scc[now]=c,in[now]=0; }while(now!=num); } inline bool check() { for(int i=1;i<=n;i++) for(int j=0;j<3;j++) for(int k=0;k<3;k++) if(j!=k) g.ad(e[i][j]+(!v[i])*n,e[i][k]+v[i]*n);
for(int i=1;i<=2*n;i++) if(!dfn[i]) dfs(i);
bool f=1; for(int i=1;i<=n;i++) if(scc[i]==scc[i+n]) f=0; memset(dfn,0,sizeof(int)*(2*n+1)); memset(low,0,sizeof(int)*(2*n+1)); tim=c=0,g.clear(2*n);
return f; } inline void solve() { n=read(); for(int i=1;i<=n;i++) e[i].clear(); for(int i=1;i<=n*3/2;i++) { int u=read(),v=read(); e[u].push_back(v); e[v].push_back(u); }
R(); while(check()) R();
for(int i=1;i<=n;i++) putchar(v[i]?'B':'W'); putchar('\n'); } int main() { int T=read(); while(T--) solve(); return 0; }
|