#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const int MAXN = 100009;
const int LOG = 17;
int n, m;
int jump[MAXN];
bool bridge[MAXN];
int dep[MAXN];
vector<pii> G[MAXN];
vector<pii> G2[MAXN];
int S[MAXN];
int E[MAXN];
pii edge[MAXN];
int ans[MAXN];
bool vis[MAXN];
int tab[MAXN];
int cnt;
int T, tin[MAXN], tout[MAXN], jp2[MAXN][LOG];
void dfs1(int x, int d, int pre) {
vis[x] = true;
dep[x] = d;
jump[x] = d;
for(auto [z, id]:G[x]) {
if(id==pre) continue;
if(!vis[z]) {
dfs1(z, d+1, id);
jump[x] = min(jump[x], jump[z]);
} else {
jump[x] = min(jump[x], dep[z]);
}
}
if(pre==-1) return;
if(jump[x] == d) bridge[pre] = true;
}
void dfs2(int x, int col) {
vis[x] = true;
tab[x] = col;
for(auto [z, id]:G[x]) {
if(!bridge[id]) {
if(!vis[z]) dfs2(z, col);
} else {
if(vis[z]) {
G2[col].pb({tab[z], id});
} else {
dfs2(z, ++cnt);
G2[col].pb({tab[z], id});
}
}
}
}
void dfs3(int x, int y) {
vis[x] = true;
tin[x] = ++T;
jp2[x][0] = y;
for(int i=1;i<LOG;i++) {
jp2[x][i] = jp2[jp2[x][i-1]][i-1];
}
for(auto [z, id]:G2[x]) {
if(z!=y) dfs3(z, x);
}
tout[x] = ++T;
}
bool is_ancestor(int x, int y) {
return tin[x]<=tin[y]&&tout[x]>=tout[y];
}
int lca(int x, int y) {
if(is_ancestor(x, y)) return x;
if(is_ancestor(y, x)) return y;
for(int i=LOG-1;i>=0;i--) {
if(!is_ancestor(jp2[x][i], y)) {
x = jp2[x][i];
}
}
return jp2[x][0];
}
pii dfs4(int x, int pre) {
vis[x] = true;
pii val = {0, 0};
for(auto [z, id]:G2[x]) {
if(id==pre) continue;
pii nval = dfs4(z, id);
val.st += nval.st;
val.nd += nval.nd;
}
val.st += S[x];
val.nd += E[x];
if(pre==-1) return val;
if(val.st) {
if(x == tab[edge[pre].st]) {
ans[pre] = -1;
} else {
ans[pre] = 1;
}
} else if(val.nd) {
if(x == tab[edge[pre].st]) {
ans[pre] = 1;
} else {
ans[pre] = -1;
}
}
return val;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i=0;i<m;i++) {
int a, b;
cin >> a >> b;
G[a].pb({b, i});
G[b].pb({a, i});
edge[i] = {a, b};
}
for(int i=1;i<=n;i++) {
if(!vis[i]) {
dfs1(i, 0, -1);
}
}
memset(vis, 0, sizeof(vis));
for(int i=1;i<=n;i++) {
if(!vis[i]) {
dfs2(i, ++cnt);
}
}
memset(vis, 0, sizeof(vis));
for(int i=1;i<=cnt;i++) {
if(!vis[i]) {
dfs3(i, 0);
}
}
tin[0] = 0;
tout[0] = ++T;
int q;
cin >> q;
while(q--) {
int a, b;
cin >> a >> b;
S[tab[a]]++;
E[tab[b]]++;
int Lca = lca(tab[a], tab[b]);
S[Lca]--;
E[Lca]--;
}
memset(vis, 0, sizeof(vis));
for(int i=1;i<=cnt;i++) {
if(!vis[i]) {
dfs4(i, -1);
}
}
for(int i=0;i<m;i++) {
if(ans[i]==0) {
cout << "B";
} else if(ans[i]==1) {
cout << "L";
} else {
cout << "R";
}
}
cout << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5300 KB |
Output is correct |
4 |
Correct |
5 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
5460 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5428 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5300 KB |
Output is correct |
4 |
Correct |
5 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
5460 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5428 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
38 ms |
13832 KB |
Output is correct |
12 |
Correct |
43 ms |
15156 KB |
Output is correct |
13 |
Correct |
69 ms |
17132 KB |
Output is correct |
14 |
Correct |
71 ms |
22168 KB |
Output is correct |
15 |
Correct |
79 ms |
24428 KB |
Output is correct |
16 |
Correct |
87 ms |
34536 KB |
Output is correct |
17 |
Correct |
84 ms |
37708 KB |
Output is correct |
18 |
Correct |
93 ms |
35532 KB |
Output is correct |
19 |
Correct |
83 ms |
39348 KB |
Output is correct |
20 |
Correct |
43 ms |
14416 KB |
Output is correct |
21 |
Correct |
42 ms |
15028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5300 KB |
Output is correct |
4 |
Correct |
5 ms |
5588 KB |
Output is correct |
5 |
Correct |
4 ms |
5460 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5428 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
38 ms |
13832 KB |
Output is correct |
12 |
Correct |
43 ms |
15156 KB |
Output is correct |
13 |
Correct |
69 ms |
17132 KB |
Output is correct |
14 |
Correct |
71 ms |
22168 KB |
Output is correct |
15 |
Correct |
79 ms |
24428 KB |
Output is correct |
16 |
Correct |
87 ms |
34536 KB |
Output is correct |
17 |
Correct |
84 ms |
37708 KB |
Output is correct |
18 |
Correct |
93 ms |
35532 KB |
Output is correct |
19 |
Correct |
83 ms |
39348 KB |
Output is correct |
20 |
Correct |
43 ms |
14416 KB |
Output is correct |
21 |
Correct |
42 ms |
15028 KB |
Output is correct |
22 |
Correct |
144 ms |
37996 KB |
Output is correct |
23 |
Correct |
123 ms |
35912 KB |
Output is correct |
24 |
Correct |
141 ms |
36044 KB |
Output is correct |
25 |
Correct |
124 ms |
42192 KB |
Output is correct |
26 |
Correct |
143 ms |
37540 KB |
Output is correct |
27 |
Correct |
141 ms |
36224 KB |
Output is correct |
28 |
Correct |
31 ms |
11032 KB |
Output is correct |
29 |
Correct |
65 ms |
14032 KB |
Output is correct |
30 |
Correct |
61 ms |
14472 KB |
Output is correct |
31 |
Correct |
66 ms |
14388 KB |
Output is correct |
32 |
Correct |
89 ms |
23876 KB |
Output is correct |