#include<bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std ;
const int maxn = 5e5 + 10 , maxq = 502 , inf = 1e9 , mod = 1e9+7 ;
vector <pair <int , int > > G[maxn] ;
vector <int> l[maxn] ;
int dp[maxn] , mark2[maxn] , mark[maxn] , par[maxn] , cnt = 1 ,c[maxn] , st[maxn] , en[maxn] , pr[maxn] , pp[maxn] , ans[maxn] , ok[maxn];
pair <int , int > ed[maxn] ;
void dfs(int v){
mark[v] = 1;
for(int i = 0; i < (int)G[v].size() ; i++){
int u = G[v][i].first , id = G[v][i].second ;
if(mark[u] == 1){
if(mark2[id] == 0){
dp[v] ++ ;
dp[u] -- ;
ans[id] = 3 ;
mark2[id] = 1;
}
continue ;
}
mark2[id] = 1;
par[u] = id ;
dfs(u) ;
dp[v]+=dp[u];
}
}
void dfs2(int v , int r){
mark[v] = 1;
c[v] = r ;
for(int i = 0; i < (int)G[v].size() ; i++){
int u = G[v][i].first , id = G[v][i].second ;
if(mark[u] == 1)continue ;
if(dp[u] == 0){
cnt ++;
//cout << v << " " << u << "<---\n";
if(ed[id].first == u){
ans[id] = 1;//l
}else{
ans[id] = 2; //r
}
pr[cnt] = id ;
pp[cnt] = r ;
l[r].pb(cnt);
dfs2(u , cnt);
}else{
dfs2(u , r);
}
}
}
int sec = 1 ;
void dfs3(int v){
mark[v] = 1;
st[v] = sec ;
sec++;
for(int i = 0 ;i < (int)l[v].size() ; i++){
int u = l[v][i] ;
dfs3(u);
}
en[v] = sec;
sec++;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
int n , m ;
cin >> n >> m ;
for(int i =1 ;i <= m ; i++){
int v , u ;
cin >> v >> u ;
if(v==u){
ans[i] = 3;
continue ;
}
G[v].pb({u,i});
G[u].pb({v,i});
ed[i] = {v , u} ;
}
for(int i = 1; i <= n ; i++){
if(mark[i] == 1)continue ;
dfs(i);
}
for(int i =1 ; i <= n ; i++){
if(par[i] == 0)continue ;
if(dp[i] >= 1){
ans[par[i]] = 3 ;
}
}
memset(mark , 0 , sizeof mark);
memset(mark2 , 0 , sizeof mark2);
vector <int> vec ;
for(int i =1; i <= n ; i ++){
if(mark[i] == 1)continue ;
vec.pb(cnt);
dfs2(i ,cnt ) ;
cnt++;
}
memset(mark , 0 , sizeof mark);
for(int i =0 ; i < (int)vec.size() ;i++){
int u = vec[i];
dfs3(u);
}
int q ;
cin >> q ;
while(q--){
int v , u ;
cin >> v >> u ;
if(c[v] == c[u])continue ;
v = c[v] ;
u = c[u] ;
while(!(st[v] <= st[u] && en[v] >= en[u])){
if(ok[pr[v]] == 0){
ans[pr[v]] = (3 - ans[pr[v]]) ;
ok[pr[v]] = 1;
}
v = pp[v] ;
}
}
for(int i = 1 ; i <= m ; i++){
if(ans[i] == 3){
cout << "B";
}
if(ans[i] == 2){
cout << "R";
}
if(ans[i] == 1){
cout << "L" ;
}
}
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |