// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
struct DSU {
vi e; void init(int N) { e = vi(N, -1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool unite(int x, int y) {
x = get(x), y = get(y); if (x == y) return 0;
e[x] += e[y]; e[y] = x; return 1;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
DSU D; D.init(N);
V<V<pi>> adj(N); V<pi> ex, E;
string ans(M, 'B');
for(int i = 0; i < M; i++) {
int u, v; cin >> u >> v; --u, --v;
E.pb(mp(u, v));
if (D.unite(u, v)) adj[u].pb(mp(v, i)), adj[v].pb(mp(u, i));
else ex.pb(mp(u, v));
}
V<pi> par(N); vi dep(N, -1);
function<void(int)> dfs = [&](int u) {
for(auto& v : adj[u]) if (v.f != par[u].f) {
par[v.f] = mp(u, v.s); dep[v.f] = dep[u] + 1; dfs(v.f);
}
};
for(int r = 0; r < N; r++) if (dep[r] == -1) {
par[r] = mp(-1, -1); dep[r] = 0; dfs(r);
}
D.init(N);
for(const auto& p : ex) {
int u, v; tie(u, v) = p;
while(1) {
int a = D.get(u), b = D.get(v);
if (a == b) break;
if (dep[a] < dep[b]) swap(a, b);
assert(par[a].f != -1); D.unite(par[a].f, a);
}
}
vi to(M, -1);
int Q; cin >> Q;
for(int i = 0; i < Q; i++) {
int u, v; cin >> u >> v; --u, --v;
if (D.get(u) == D.get(v)) continue;
while(1) {
int a = D.get(u), b = D.get(v);
if (a == b) break;
if (dep[a] < dep[b]) swap(a, b);
to[par[a].s] = (D.get(u) == a ? par[a].f : a);
assert(par[a].f != -1); D.unite(par[a].f, a);
}
}
for(int i = 0; i < M; i++) if (to[i] != -1) {
ans[i] = (E[i].f == to[i] ? 'L' : 'R');
}
cout << ans << nl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
30 ms |
3804 KB |
Output is correct |
12 |
Correct |
25 ms |
4624 KB |
Output is correct |
13 |
Correct |
28 ms |
5816 KB |
Output is correct |
14 |
Correct |
33 ms |
7640 KB |
Output is correct |
15 |
Correct |
46 ms |
8356 KB |
Output is correct |
16 |
Correct |
37 ms |
9284 KB |
Output is correct |
17 |
Correct |
51 ms |
11112 KB |
Output is correct |
18 |
Correct |
65 ms |
9252 KB |
Output is correct |
19 |
Correct |
34 ms |
12532 KB |
Output is correct |
20 |
Correct |
32 ms |
5840 KB |
Output is correct |
21 |
Correct |
26 ms |
6152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
30 ms |
3804 KB |
Output is correct |
12 |
Correct |
25 ms |
4624 KB |
Output is correct |
13 |
Correct |
28 ms |
5816 KB |
Output is correct |
14 |
Correct |
33 ms |
7640 KB |
Output is correct |
15 |
Correct |
46 ms |
8356 KB |
Output is correct |
16 |
Correct |
37 ms |
9284 KB |
Output is correct |
17 |
Correct |
51 ms |
11112 KB |
Output is correct |
18 |
Correct |
65 ms |
9252 KB |
Output is correct |
19 |
Correct |
34 ms |
12532 KB |
Output is correct |
20 |
Correct |
32 ms |
5840 KB |
Output is correct |
21 |
Correct |
26 ms |
6152 KB |
Output is correct |
22 |
Correct |
50 ms |
11124 KB |
Output is correct |
23 |
Correct |
59 ms |
9204 KB |
Output is correct |
24 |
Correct |
54 ms |
9256 KB |
Output is correct |
25 |
Correct |
67 ms |
14704 KB |
Output is correct |
26 |
Correct |
47 ms |
10632 KB |
Output is correct |
27 |
Correct |
49 ms |
9320 KB |
Output is correct |
28 |
Correct |
20 ms |
2408 KB |
Output is correct |
29 |
Correct |
38 ms |
5804 KB |
Output is correct |
30 |
Correct |
36 ms |
5912 KB |
Output is correct |
31 |
Correct |
37 ms |
5824 KB |
Output is correct |
32 |
Correct |
40 ms |
10176 KB |
Output is correct |