#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1e5;
int n, m, k, eu[mxN], ev[mxN], t, ecc, tin[mxN], low[mxN], who[mxN], d[mxN], anc[mxN][17], dp[mxN][2];
vector<int> adj[mxN];
stack<int> st;
vector<ar<int, 2>> br, adj2[mxN];
char ans[mxN];
void make(int u) {
who[u]=ecc;
while(st.top()^u)
who[st.top()]=ecc, st.pop();
st.pop(), ++ecc;
}
void dfs(int u, int p=-1) {
tin[u]=low[u]=++t;
st.push(u);
for (int i : adj[u]) {
if (i==p)
continue;
int v=u^eu[i]^ev[i];
if (!tin[v]) {
dfs(v, i);
low[u]=min(low[u], low[v]);
if (low[v]>tin[u]) {
br.push_back({i, u==eu[i]});
make(v);
}
} else
low[u]=min(low[u], tin[v]);
}
}
void dfs2(int u, int p=-1) {
anc[u][0]=p;
for (int i=1; i<17; ++i)
anc[u][i]=anc[u][i-1]^-1?anc[anc[u][i-1]][i-1]:-1;
for (ar<int, 2> x : adj2[u]) {
int v=u^who[eu[x[0]]]^who[ev[x[0]]];
d[v]=d[u]+1;
dfs2(v, u);
}
}
int lca(int u, int v) {
if (d[u]>d[v])
swap(u, v);
for (int i=16; ~i; --i)
if (d[u]<=d[v]-(1<<i))
v=anc[v][i];
if (u==v)
return u;
for (int i=16; ~i; --i)
if (anc[u][i]^anc[v][i])
u=anc[u][i], v=anc[v][i];
return anc[u][0];
}
void dfs3(int u) {
for (ar<int, 2> x : adj2[u]) {
int v=u^who[eu[x[0]]]^who[ev[x[0]]];
dfs3(v);
dp[u][0]+=dp[v][0];
dp[u][1]+=dp[v][1];
assert(!(dp[v][0]&&dp[v][1]));
if (dp[v][0]||dp[v][1])
ans[x[0]]=x[1]^(dp[v][0]>0)?'R':'L';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<m; ++i) {
cin >> eu[i] >> ev[i], --eu[i], --ev[i];
if (eu[i]^ev[i]) {
adj[eu[i]].push_back(i);
adj[ev[i]].push_back(i);
}
}
for (int i=0; i<n; ++i) {
if (tin[i])
continue;
dfs(i);
make(i);
for (ar<int, 2> b : br) {
int u=b[1]?eu[b[0]]:ev[b[0]];
adj2[who[u]].push_back(b);
}
br.clear();
dfs2(who[i]);
}
cin >> k;
for (int i=0; i<k; ++i) {
int u, v;
cin >> u >> v, --u, --v;
u=who[u], v=who[v];
int uv=lca(u, v);
++dp[u][0], --dp[uv][0];
++dp[v][1], --dp[uv][1];
}
memset(ans, 'B', m);
for (int i=0; i<ecc; ++i)
if (anc[i][0]==-1)
dfs3(i);
for (int i=0; i<m; ++i)
cout << ans[i];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5196 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5196 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
44 ms |
10560 KB |
Output is correct |
12 |
Correct |
48 ms |
11812 KB |
Output is correct |
13 |
Correct |
59 ms |
13628 KB |
Output is correct |
14 |
Correct |
75 ms |
16484 KB |
Output is correct |
15 |
Correct |
68 ms |
17748 KB |
Output is correct |
16 |
Correct |
94 ms |
21944 KB |
Output is correct |
17 |
Correct |
88 ms |
24620 KB |
Output is correct |
18 |
Correct |
93 ms |
21944 KB |
Output is correct |
19 |
Correct |
85 ms |
26276 KB |
Output is correct |
20 |
Correct |
49 ms |
11560 KB |
Output is correct |
21 |
Correct |
46 ms |
11224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5196 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
44 ms |
10560 KB |
Output is correct |
12 |
Correct |
48 ms |
11812 KB |
Output is correct |
13 |
Correct |
59 ms |
13628 KB |
Output is correct |
14 |
Correct |
75 ms |
16484 KB |
Output is correct |
15 |
Correct |
68 ms |
17748 KB |
Output is correct |
16 |
Correct |
94 ms |
21944 KB |
Output is correct |
17 |
Correct |
88 ms |
24620 KB |
Output is correct |
18 |
Correct |
93 ms |
21944 KB |
Output is correct |
19 |
Correct |
85 ms |
26276 KB |
Output is correct |
20 |
Correct |
49 ms |
11560 KB |
Output is correct |
21 |
Correct |
46 ms |
11224 KB |
Output is correct |
22 |
Correct |
154 ms |
25788 KB |
Output is correct |
23 |
Correct |
155 ms |
23392 KB |
Output is correct |
24 |
Correct |
138 ms |
23188 KB |
Output is correct |
25 |
Correct |
187 ms |
30112 KB |
Output is correct |
26 |
Correct |
154 ms |
25168 KB |
Output is correct |
27 |
Correct |
139 ms |
23592 KB |
Output is correct |
28 |
Correct |
44 ms |
8292 KB |
Output is correct |
29 |
Correct |
78 ms |
12068 KB |
Output is correct |
30 |
Correct |
75 ms |
12348 KB |
Output is correct |
31 |
Correct |
72 ms |
12828 KB |
Output is correct |
32 |
Correct |
102 ms |
18916 KB |
Output is correct |