이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Remember...
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=1e5+10,mod=1e9+7;
const ll inf=1e18;
vector<pii> v[maxn], g[maxn];
int C, h[maxn], hh[maxn];
bool mark[maxn];
int ans[maxn], who[maxn];
void prep(int u,int par=-1){
mark[u]=1;
hh[u]= h[u];
for(auto p:v[u]){
if(p.S!=par){
if(mark[p.F])
hh[u]=min(hh[u], h[p.F]);
else
h[p.F]= h[u]+1, prep(p.F,p.S), hh[u]=min(hh[u], hh[p.F]);
}
}
}
void buildg(int u,int id){
who[u]=id;
mark[u]=1;
for(auto p:v[u]){
if(mark[p.F]==0){
if(hh[p.F]==h[p.F])
g[id].PB({++C,p.S}), buildg(p.F,C);
else
buildg(p.F,id);
}
}
}
int sp[20][maxn];
int h2[maxn];
int cntd[maxn], cntu[maxn];
void prep2(int u,int par=0){
mark[u]=1;
h2[u]= h2[par]+1;
sp[0][u]= par;
for(int i=1;i<20;i++)
sp[i][u]= sp[i-1][sp[i-1][u]];
for(auto p:g[u])
prep2(p.F,u);
}
int lca(int a,int b){
if(h2[a]<h2[b]) swap(a,b);
for(int i=19;i>=0;i--)
if(h2[sp[i][a]]>=h2[b]) a=sp[i][a];
if(a==b) return a;
for(int i=19;i>=0;i--)
if(sp[i][a]!=sp[i][b]) a=sp[i][a], b=sp[i][b];
return sp[0][a];
}
void dfs(int u,int par=-1){
mark[u]=1;
for(auto p:g[u]){
dfs(p.F,p.S);
cntu[u]+= cntu[p.F];
cntd[u]+= cntd[p.F];
}
assert((cntu[u] && cntd[u])==0);
if(cntu[u])
ans[par]=1;
if(cntd[u])
ans[par]=0;
}
vector<pii> ed;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
memset(ans,-1,sizeof ans);
int n,m; cin>>n>>m;
for(int i=0;i<m;i++){
int a,b; cin>>a>>b;
v[a].PB({b,i});
v[b].PB({a,i});
ed.PB({a,b});
}
for(int i=1;i<=n;i++)
if(mark[i]==0)
prep(i);
memset(mark,0,sizeof mark);
for(int i=1;i<=n;i++)
if(mark[i]==0)
buildg(i,++C);
memset(mark,0,sizeof mark);
for(int i=1;i<=C;i++)
if(mark[i]==0)
prep2(i);
int q; cin>>q;
for(int i=0;i<q;i++){
int a,b; cin>>a>>b;
a=who[a], b=who[b];
int lc=lca(a,b);
cntu[a]++, cntu[lc]--;
cntd[b]++, cntd[lc]--;
}
memset(mark,0,sizeof mark);
for(int i=1;i<=C;i++)
if(mark[i]==0)
dfs(i);
for(int i=0;i<m;i++){
if(ans[i]==-1) cout<<'B';
else if((ans[i]) ^ (h[ed[i].F] > h[ed[i].S])) cout<<'L';
else cout<<'R';
}
return cout<<endl,0;
}
// Deathly mistakes:
// * Read the problem carefully.
// * Check maxn.
// * Overflows.
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |