#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int n, m, p;
const int MAX_M = 1e5;
const int MAX_N = 1e5;
char status[MAX_M];
struct Node{
vector<int> adj;
vector<int> id;
vector<int> dir;
bool vis =false;
int depth = 0;
int open = 0;
} g[MAX_N], g2[MAX_N];
int dsu[MAX_N];
int sz[MAX_N];
int get(int id){
vector<int> path;
while(dsu[id]!=id){
path.push_back(id);
id = dsu[id];
}
for(auto e: path){
dsu[e] = id;
}
return id;
}
void merge(int a, int b){
////cout<<a<<" and "<<b<<endl;
a = get(a);
b= get(b);
if(sz[a]<sz[b]){
dsu[a] = b;
sz[b] += sz[a];
}
else{
dsu[b] = a;
sz[a] += sz[b];
}
}
int dfs(int u, int depth, int anc){
//////cout<<"dfs "<<u<<" "<<depth<<endl;
g[u].vis = true;
g[u].depth = depth;
int r = depth;
for(int i= 0; i<g[u].adj.size(); i++){
int v = g[u].adj[i];
if(g[u].id[i] != anc){
if(!g[v].vis){
int val =dfs(v, depth+1, g[u].id[i]);
if(val<=depth){
merge(u, v);
}
r = min(r, val);
}
else{
int val = g[v].depth;
if(val<=depth){
merge(u, v);
}
r = min(r, val);
}
}
}
return r;
}
pii edges[MAX_M];
const int MAX_P = 1e5;
pii pairs[MAX_P];
int dfs2(int id, int anc){
g2[id].vis = true;
int s= 0;
for(int i = 0; i<g2[id].adj.size(); i++){
if(g2[id].id[i] != anc){
int c_s = dfs2(g2[id].adj[i], g2[id].id[i]);
//cout<<g2[id].id[i]<<" "<<g2[id].dir[i]<<" "<<c_s<<endl;
if(c_s ==0){
status[g2[id].id[i]] = 'B';
}
else if(c_s >0){
if(g2[id].dir[i] == -1){
status[g2[id].id[i]] = 'R';
}
else{
status[g2[id].id[i]] = 'L';
}
}
else{
if(g2[id].dir[i] == -1){
status[g2[id].id[i]] = 'L';
}
else{
status[g2[id].id[i]] = 'R';
}
}
s+=c_s;
}
}
s+= g2[id].open;
//cout<<id<<" "<<s<<endl;
return s;
}
signed main(){
cin>>n>>m;
for(int i = 0; i<n; i++){
dsu[i] = i;
sz[i]=1;
}
for(int i = 0; i<m; i++){
int a, b;
cin>>a>>b;
a--;b--;
g[a].adj.push_back(b);
g[b].adj.push_back(a);
g[a].id.push_back(i);
g[b].id.push_back(i);
g[a].dir.push_back(1);
g[b].dir.push_back(-1);
edges[i].first = a;
edges[i].second = b;
}
for(int i = 0; i<n; i++){
if(!g[i].vis){
dfs(i, 0, -1);
}
}
////cout<<"dfs done"<<endl;
for(int i = 0; i<n; i++){
//cout<<get(i)<<endl;
}
for(int i = 0; i<m; i++){
if(get(edges[i].first) ==get(edges[i].second)){
status[i] = 'B';
////cout<<i<<" ok"<<endl;
}
else{
int a = get(edges[i].first);
int b= get(edges[i].second);
g2[a].adj.push_back(b);
g2[b].adj.push_back(a);
g2[a].id.push_back(i);
g2[b].id.push_back(i);
g2[a].dir.push_back(1);
g2[b].dir.push_back(-1);
}
}
cin>>p;
for(int i = 0; i<p; i++){
cin>>pairs[i].first>>pairs[i].second;
pairs[i].first--;pairs[i].second--;
g2[get(pairs[i].first)].open ++;
g2[get(pairs[i].second)].open--;
}
for(int i = 0; i<n; i++){
if(!g2[get(i)].vis){
dfs2(get(i), -1);
}
}
for(int i = 0; i<m; i++){
cout<<status[i];
}
cout<<endl;
}
Compilation message
oneway.cpp: In function 'long long int dfs(long long int, long long int, long long int)':
oneway.cpp:61:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i= 0; i<g[u].adj.size(); i++){
| ~^~~~~~~~~~~~~~~~
oneway.cpp: In function 'long long int dfs2(long long int, long long int)':
oneway.cpp:92:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i = 0; i<g2[id].adj.size(); i++){
| ~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23644 KB |
Output is correct |
2 |
Correct |
5 ms |
23644 KB |
Output is correct |
3 |
Correct |
5 ms |
23644 KB |
Output is correct |
4 |
Correct |
5 ms |
23724 KB |
Output is correct |
5 |
Correct |
5 ms |
23900 KB |
Output is correct |
6 |
Correct |
5 ms |
23752 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
6 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23644 KB |
Output is correct |
10 |
Correct |
6 ms |
23644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23644 KB |
Output is correct |
2 |
Correct |
5 ms |
23644 KB |
Output is correct |
3 |
Correct |
5 ms |
23644 KB |
Output is correct |
4 |
Correct |
5 ms |
23724 KB |
Output is correct |
5 |
Correct |
5 ms |
23900 KB |
Output is correct |
6 |
Correct |
5 ms |
23752 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
6 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23644 KB |
Output is correct |
10 |
Correct |
6 ms |
23644 KB |
Output is correct |
11 |
Correct |
87 ms |
33004 KB |
Output is correct |
12 |
Correct |
91 ms |
34132 KB |
Output is correct |
13 |
Correct |
113 ms |
36688 KB |
Output is correct |
14 |
Correct |
122 ms |
39376 KB |
Output is correct |
15 |
Correct |
126 ms |
40272 KB |
Output is correct |
16 |
Correct |
172 ms |
47444 KB |
Output is correct |
17 |
Correct |
157 ms |
47956 KB |
Output is correct |
18 |
Correct |
162 ms |
47624 KB |
Output is correct |
19 |
Correct |
148 ms |
49232 KB |
Output is correct |
20 |
Correct |
94 ms |
34388 KB |
Output is correct |
21 |
Correct |
93 ms |
34128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23644 KB |
Output is correct |
2 |
Correct |
5 ms |
23644 KB |
Output is correct |
3 |
Correct |
5 ms |
23644 KB |
Output is correct |
4 |
Correct |
5 ms |
23724 KB |
Output is correct |
5 |
Correct |
5 ms |
23900 KB |
Output is correct |
6 |
Correct |
5 ms |
23752 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
6 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23644 KB |
Output is correct |
10 |
Correct |
6 ms |
23644 KB |
Output is correct |
11 |
Correct |
87 ms |
33004 KB |
Output is correct |
12 |
Correct |
91 ms |
34132 KB |
Output is correct |
13 |
Correct |
113 ms |
36688 KB |
Output is correct |
14 |
Correct |
122 ms |
39376 KB |
Output is correct |
15 |
Correct |
126 ms |
40272 KB |
Output is correct |
16 |
Correct |
172 ms |
47444 KB |
Output is correct |
17 |
Correct |
157 ms |
47956 KB |
Output is correct |
18 |
Correct |
162 ms |
47624 KB |
Output is correct |
19 |
Correct |
148 ms |
49232 KB |
Output is correct |
20 |
Correct |
94 ms |
34388 KB |
Output is correct |
21 |
Correct |
93 ms |
34128 KB |
Output is correct |
22 |
Correct |
205 ms |
49312 KB |
Output is correct |
23 |
Correct |
188 ms |
47952 KB |
Output is correct |
24 |
Correct |
189 ms |
48748 KB |
Output is correct |
25 |
Correct |
179 ms |
52052 KB |
Output is correct |
26 |
Correct |
199 ms |
48940 KB |
Output is correct |
27 |
Correct |
183 ms |
48124 KB |
Output is correct |
28 |
Correct |
73 ms |
30992 KB |
Output is correct |
29 |
Correct |
138 ms |
35404 KB |
Output is correct |
30 |
Correct |
133 ms |
35412 KB |
Output is correct |
31 |
Correct |
149 ms |
35940 KB |
Output is correct |
32 |
Correct |
165 ms |
40412 KB |
Output is correct |