#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500'001;
const int K = 21;
vector<pair<int, int>> g[MAXN], g2[MAXN];
int l[MAXN], tin[MAXN], timer = 1, cc = 0, comp[MAXN], be[MAXN], ki[MAXN], up[MAXN][K], fel[MAXN], le[MAXN], U[MAXN], V[MAXN];
char ans[MAXN];
bool is_bridge[MAXN], vis[MAXN];
void dfs(int u, int p = -1) {
l[u] = tin[u] = timer++;
vis[u] = 1;
for (auto [v, idx] : g[u]) {
if (idx == p) continue;
if (tin[v]) {
l[u] = min(l[u], tin[v]);
} else {
dfs(v, idx);
l[u] = min(l[u], l[v]);
if (l[v] > tin[u]) {
is_bridge[idx] = 1;
}
}
}
}
void dfs2(int u) {
vis[u] = 1;
comp[u] = cc;
for (auto [v, idx] : g[u]) {
if (is_bridge[idx] || comp[v]) continue;
dfs2(v);
}
}
void dfs3(int u, int p = -1) {
vis[u] = 1;
if (p != -1) {
up[u][0] = p;
}
be[u] = timer++;
for (auto [v, idx] : g2[u]) {
if (v == p) continue;
dfs3(v, u);
}
ki[u] = timer++;
}
bool is_ancestor(int u, int v) {
return be[u] <= be[v] && ki[v] <= ki[u];
}
int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i = K-1; i >= 0; i--) {
if (up[u][i] && !is_ancestor(up[u][i], v)) u = up[u][i];
}
return up[u][0];
}
pair<int, int> dfs4(int u, int p = -1) {
vis[u] = 1;
int x = 0, y = 0;
for (auto [v, idx] : g2[u]) {
if (v == p) continue;
auto [a, b] = dfs4(v, u);
if (a == 0 && b == 0) ans[idx] = 'B';
else if (a) ans[idx] = v == comp[V[idx]] ? 'L' : 'R';
else if (b) ans[idx] = v == comp[V[idx]] ? 'R' : 'L';
x += a; y += b;
}
return {x + fel[u], y + le[u]};
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> U[i] >> V[i];
g[U[i]].emplace_back(V[i], i);
g[V[i]].emplace_back(U[i], i);
}
fill(vis, vis+MAXN, 0);
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 1; i <= n; i++) {
if (!comp[i]) {
cc++;
dfs2(i);
}
}
for (int i = 1; i <= n; i++) {
for (auto [v, idx] : g[i]) {
if (comp[v] != comp[i]) {
g2[comp[i]].emplace_back(comp[v], idx);
} else {
ans[idx] = 'B';
}
}
}
fill(vis, vis+MAXN, 0);
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs3(i);
}
for (int k = 1; k < K; k++) {
for (int i = 1; i <= cc; i++) {
up[i][k] = up[up[i][k-1]][k-1];
}
}
int q; cin >> q;
while (q--) {
int u, v; cin >> u >> v;
u = comp[u]; v = comp[v];
int x = lca(u, v);
fel[u]++;
le[v]++;
fel[x]--;
le[x]--;
}
fill(vis, vis+MAXN, 0);
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs4(i);
}
for (int i = 1; i <= m; i++) {
cout << ans[i];
}
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24412 KB |
Output is correct |
2 |
Correct |
11 ms |
24512 KB |
Output is correct |
3 |
Correct |
9 ms |
24616 KB |
Output is correct |
4 |
Correct |
12 ms |
24668 KB |
Output is correct |
5 |
Correct |
18 ms |
24668 KB |
Output is correct |
6 |
Correct |
10 ms |
24408 KB |
Output is correct |
7 |
Correct |
13 ms |
24664 KB |
Output is correct |
8 |
Correct |
16 ms |
24668 KB |
Output is correct |
9 |
Correct |
11 ms |
24424 KB |
Output is correct |
10 |
Correct |
10 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24412 KB |
Output is correct |
2 |
Correct |
11 ms |
24512 KB |
Output is correct |
3 |
Correct |
9 ms |
24616 KB |
Output is correct |
4 |
Correct |
12 ms |
24668 KB |
Output is correct |
5 |
Correct |
18 ms |
24668 KB |
Output is correct |
6 |
Correct |
10 ms |
24408 KB |
Output is correct |
7 |
Correct |
13 ms |
24664 KB |
Output is correct |
8 |
Correct |
16 ms |
24668 KB |
Output is correct |
9 |
Correct |
11 ms |
24424 KB |
Output is correct |
10 |
Correct |
10 ms |
24412 KB |
Output is correct |
11 |
Correct |
38 ms |
30800 KB |
Output is correct |
12 |
Correct |
46 ms |
31824 KB |
Output is correct |
13 |
Correct |
48 ms |
33620 KB |
Output is correct |
14 |
Correct |
61 ms |
37052 KB |
Output is correct |
15 |
Correct |
62 ms |
38484 KB |
Output is correct |
16 |
Correct |
70 ms |
44376 KB |
Output is correct |
17 |
Correct |
72 ms |
46420 KB |
Output is correct |
18 |
Correct |
67 ms |
44884 KB |
Output is correct |
19 |
Correct |
76 ms |
48216 KB |
Output is correct |
20 |
Correct |
42 ms |
31576 KB |
Output is correct |
21 |
Correct |
38 ms |
31308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24412 KB |
Output is correct |
2 |
Correct |
11 ms |
24512 KB |
Output is correct |
3 |
Correct |
9 ms |
24616 KB |
Output is correct |
4 |
Correct |
12 ms |
24668 KB |
Output is correct |
5 |
Correct |
18 ms |
24668 KB |
Output is correct |
6 |
Correct |
10 ms |
24408 KB |
Output is correct |
7 |
Correct |
13 ms |
24664 KB |
Output is correct |
8 |
Correct |
16 ms |
24668 KB |
Output is correct |
9 |
Correct |
11 ms |
24424 KB |
Output is correct |
10 |
Correct |
10 ms |
24412 KB |
Output is correct |
11 |
Correct |
38 ms |
30800 KB |
Output is correct |
12 |
Correct |
46 ms |
31824 KB |
Output is correct |
13 |
Correct |
48 ms |
33620 KB |
Output is correct |
14 |
Correct |
61 ms |
37052 KB |
Output is correct |
15 |
Correct |
62 ms |
38484 KB |
Output is correct |
16 |
Correct |
70 ms |
44376 KB |
Output is correct |
17 |
Correct |
72 ms |
46420 KB |
Output is correct |
18 |
Correct |
67 ms |
44884 KB |
Output is correct |
19 |
Correct |
76 ms |
48216 KB |
Output is correct |
20 |
Correct |
42 ms |
31576 KB |
Output is correct |
21 |
Correct |
38 ms |
31308 KB |
Output is correct |
22 |
Correct |
113 ms |
47956 KB |
Output is correct |
23 |
Correct |
106 ms |
46008 KB |
Output is correct |
24 |
Correct |
87 ms |
46160 KB |
Output is correct |
25 |
Correct |
113 ms |
51496 KB |
Output is correct |
26 |
Correct |
104 ms |
47540 KB |
Output is correct |
27 |
Correct |
101 ms |
46164 KB |
Output is correct |
28 |
Correct |
31 ms |
28752 KB |
Output is correct |
29 |
Correct |
51 ms |
32396 KB |
Output is correct |
30 |
Correct |
52 ms |
32600 KB |
Output is correct |
31 |
Correct |
48 ms |
32624 KB |
Output is correct |
32 |
Correct |
69 ms |
38484 KB |
Output is correct |