#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500'001;
const int K = 21;
vector<pair<int, int>> g[MAXN];
map<int, vector<int>> 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];
void dfs(int u, int p = -1) {
l[u] = tin[u] = timer++;
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) {
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) {
if (p != -1) {
up[u][0] = p;
}
be[u] = timer++;
for (auto [v, indices] : 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) {
int x = 0, y = 0;
for (auto [v, indices] : g2[u]) {
if (v == p) continue;
auto [a, b] = dfs4(v, u);
if (a == 0 && b == 0) for (int idx : indices) ans[idx] = 'B';
else if (a) for (int idx : indices) ans[idx] = v == comp[V[idx]] ? 'L' : 'R';
else if (b) for (int idx : indices) 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);
}
dfs(1);
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]][comp[v]].emplace_back(idx);
} else {
ans[idx] = 'B';
}
}
}
dfs3(1);
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]--;
}
dfs4(1);
for (int i = 1; i <= m; i++) {
cout << ans[i];
}
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
35672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
35672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
35672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |