// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;
int n, m, p, h[maxn], dp[maxn], sz[maxn], par[maxn], val[maxn];
vector<pair<int, pii>> adj[maxn], g[maxn];
pii ed[maxn];
char ans[maxn];
bool mark[maxn], cut[maxn];
int find(int u){
return u == par[u] ? u : par[u] = find(par[u]);
}
void merge(int u, int v){
u = find(u);
v = find(v);
if(u == v) return;
if(sz[u] > sz[v]) swap(u, v);
par[u] = v;
sz[v] += sz[u];
}
void dfs_cut(int v, int p){
dp[v] = h[v];
mark[v] = 1;
for(auto [u, e]: adj[v]){
if(p == e.F) continue;
if(!mark[u]){
h[u] = h[v] + 1;
dfs_cut(u, e.F);
if(dp[u] > h[v]) cut[e.F] = 1;
}
smin(dp[v], dp[u]);
}
}
void dfs(int v, int p = -1, int pe = -1, int d = -1){
mark[v] = 1;
for(auto [u, e]: g[v]){
if(u == p) continue;
dfs(u, v, e.F, e.S);
val[v] += val[u];
}
if(p == -1) return;
if(val[v] == 0){
ans[pe] = 'B';
return;
}
if(val[v] < 0){
if(d == 1) ans[pe] = 'R';
else ans[pe] = 'L';
}
else{
if(d == 0) ans[pe] = 'R';
else ans[pe] = 'L';
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
par[i] = i;
sz[i] = 1;
}
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
adj[u].pb(mp(v, mp(i, 1)));
adj[v].pb(mp(u, mp(i, 0)));
ed[i] = mp(u, v);
}
for(int i = 1; i <= n; i++){
if(!mark[i]) dfs_cut(i, -1);
}
for(int i = 0; i < m; i++){
if(cut[i]) continue;
ans[i] = 'B';
merge(ed[i].F, ed[i].S);
}
for(int i = 0; i < m; i++){
if(!cut[i]) continue;
int v = find(ed[i].F), u = find(ed[i].S);
g[v].pb(mp(u, mp(i, 1)));
g[u].pb(mp(v, mp(i, 0)));
}
cin >> p;
for(int i = 0; i < p; i++){
int u, v; cin >> u >> v;
u = find(u), v = find(v);
val[u]++;
val[v]--;
}
fill(mark + 1, mark + n + 1, 0);
for(int i = 1; i <= n; i++){
if(!mark[find(i)]) dfs(find(i));
}
for(int i = 0; i < m; i++) cout << ans[i];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
28 ms |
13148 KB |
Output is correct |
12 |
Correct |
30 ms |
13908 KB |
Output is correct |
13 |
Correct |
31 ms |
14928 KB |
Output is correct |
14 |
Correct |
38 ms |
16212 KB |
Output is correct |
15 |
Correct |
45 ms |
16724 KB |
Output is correct |
16 |
Correct |
48 ms |
17912 KB |
Output is correct |
17 |
Correct |
45 ms |
19024 KB |
Output is correct |
18 |
Correct |
42 ms |
17744 KB |
Output is correct |
19 |
Correct |
47 ms |
19964 KB |
Output is correct |
20 |
Correct |
30 ms |
13392 KB |
Output is correct |
21 |
Correct |
27 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
28 ms |
13148 KB |
Output is correct |
12 |
Correct |
30 ms |
13908 KB |
Output is correct |
13 |
Correct |
31 ms |
14928 KB |
Output is correct |
14 |
Correct |
38 ms |
16212 KB |
Output is correct |
15 |
Correct |
45 ms |
16724 KB |
Output is correct |
16 |
Correct |
48 ms |
17912 KB |
Output is correct |
17 |
Correct |
45 ms |
19024 KB |
Output is correct |
18 |
Correct |
42 ms |
17744 KB |
Output is correct |
19 |
Correct |
47 ms |
19964 KB |
Output is correct |
20 |
Correct |
30 ms |
13392 KB |
Output is correct |
21 |
Correct |
27 ms |
13148 KB |
Output is correct |
22 |
Correct |
57 ms |
20048 KB |
Output is correct |
23 |
Correct |
65 ms |
18512 KB |
Output is correct |
24 |
Correct |
66 ms |
18860 KB |
Output is correct |
25 |
Correct |
63 ms |
23320 KB |
Output is correct |
26 |
Correct |
65 ms |
19792 KB |
Output is correct |
27 |
Correct |
58 ms |
18592 KB |
Output is correct |
28 |
Correct |
23 ms |
10840 KB |
Output is correct |
29 |
Correct |
43 ms |
14088 KB |
Output is correct |
30 |
Correct |
41 ms |
14160 KB |
Output is correct |
31 |
Correct |
40 ms |
14420 KB |
Output is correct |
32 |
Correct |
47 ms |
17352 KB |
Output is correct |