#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 100;
const int MAX_LOG = 18;
vector<pair<int, int>> a[MAXN];
vector<pair<int, int>> bridges[MAXN];
vector<pair<int, int>> edges;
int parent[MAXN][MAX_LOG];
int tin[MAXN];
int tout[MAXN];
int components = 0;
int counter = 0;
int bridge = 0;
int num[MAXN];
int low[MAXN];
bool isBridge[MAXN];
int vis[MAXN];
int comp[MAXN];
int cnt[MAXN];
int ans[MAXN];
int inv[MAXN];
int n, m, p;
void dfs(int u, int p, int f){
num[u] = low[u] = ++counter;
for (auto x: a[u]){
int v = x.first;
if (x.second == f) continue;
if (!num[v]){
dfs(v, u, x.second);
low[u] = min(low[u], low[v]);
if (low[v] == num[v]){
bridge++;
isBridge[x.second] = true;
}
}
else low[u] = min(low[u], num[v]);
}
}
void dfs1(int u, int p){
comp[u] = components;
vis[u] = 1;
for (auto v: a[u]){
if (vis[v.first] || isBridge[v.second]) continue;
dfs1(v.first, u);
}
}
void dfs2(int u, int p){
parent[u][0] = p;
vis[u] = 1;
for (auto x: bridges[u]){
int v = x.first;
if (v != p){
inv[x.second] = (u != comp[edges[x.second].first]);
dfs2(v, u);
}
}
}
void pre(){
counter = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= components; i++){
if (!vis[i]) dfs2(i, i);
}
}
void dfs3(int u, int p){
vis[u] = 1;
for (auto x: bridges[u]){
int v = x.first;
if (v == p) continue;
dfs3(v, u);
cnt[u] += cnt[v];
if (cnt[v] > 0){
ans[x.second] = 1;
}
else if (cnt[v] < 0){
ans[x.second] = -1;
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
a[x].push_back({y, i});
a[y].push_back({x, i});
edges.push_back({x, y});
}
counter = 0;
for (int i = 1; i <= n; i++){
if (!num[i]){
dfs(i, -1, -1);
}
}
for (int i = 1; i <= n; i++){
if (!vis[i]){
components++;
dfs1(i, -1);
}
}
for (int i = 0; i < m; i++){
if (isBridge[i]){
bridges[comp[edges[i].first]].push_back({comp[edges[i].second], i});
bridges[comp[edges[i].second]].push_back({comp[edges[i].first], i});
}
}
pre();
cin >> p;
while (p--){
int x, y;
cin >> x >> y;
x = comp[x];
y = comp[y];
cnt[x]++;
cnt[y]--;
}
memset(vis,0 ,sizeof(vis));
for (int i = 1; i <= components; i++){
if (!vis[i]) dfs3(i, -1);
}
for (int i = 0; i < m; i++){
if (inv[i]) ans[i] *= (-1);
if (ans[i] == -1) cout << "R";
else if (ans[i] == 1) cout << "L";
else cout << "B";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5680 KB |
Output is correct |
4 |
Correct |
3 ms |
5728 KB |
Output is correct |
5 |
Correct |
3 ms |
5720 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5680 KB |
Output is correct |
4 |
Correct |
3 ms |
5728 KB |
Output is correct |
5 |
Correct |
3 ms |
5720 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5544 KB |
Output is correct |
11 |
Correct |
32 ms |
11688 KB |
Output is correct |
12 |
Correct |
32 ms |
13256 KB |
Output is correct |
13 |
Correct |
35 ms |
14792 KB |
Output is correct |
14 |
Correct |
45 ms |
17608 KB |
Output is correct |
15 |
Correct |
46 ms |
18964 KB |
Output is correct |
16 |
Correct |
53 ms |
24492 KB |
Output is correct |
17 |
Correct |
48 ms |
25804 KB |
Output is correct |
18 |
Correct |
51 ms |
24520 KB |
Output is correct |
19 |
Correct |
49 ms |
26900 KB |
Output is correct |
20 |
Correct |
27 ms |
12228 KB |
Output is correct |
21 |
Correct |
28 ms |
12784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5680 KB |
Output is correct |
4 |
Correct |
3 ms |
5728 KB |
Output is correct |
5 |
Correct |
3 ms |
5720 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5544 KB |
Output is correct |
11 |
Correct |
32 ms |
11688 KB |
Output is correct |
12 |
Correct |
32 ms |
13256 KB |
Output is correct |
13 |
Correct |
35 ms |
14792 KB |
Output is correct |
14 |
Correct |
45 ms |
17608 KB |
Output is correct |
15 |
Correct |
46 ms |
18964 KB |
Output is correct |
16 |
Correct |
53 ms |
24492 KB |
Output is correct |
17 |
Correct |
48 ms |
25804 KB |
Output is correct |
18 |
Correct |
51 ms |
24520 KB |
Output is correct |
19 |
Correct |
49 ms |
26900 KB |
Output is correct |
20 |
Correct |
27 ms |
12228 KB |
Output is correct |
21 |
Correct |
28 ms |
12784 KB |
Output is correct |
22 |
Correct |
64 ms |
26936 KB |
Output is correct |
23 |
Correct |
65 ms |
25288 KB |
Output is correct |
24 |
Correct |
63 ms |
25540 KB |
Output is correct |
25 |
Correct |
62 ms |
29860 KB |
Output is correct |
26 |
Correct |
62 ms |
26656 KB |
Output is correct |
27 |
Correct |
69 ms |
25372 KB |
Output is correct |
28 |
Correct |
21 ms |
9680 KB |
Output is correct |
29 |
Correct |
37 ms |
12996 KB |
Output is correct |
30 |
Correct |
36 ms |
13476 KB |
Output is correct |
31 |
Correct |
39 ms |
13328 KB |
Output is correct |
32 |
Correct |
49 ms |
19140 KB |
Output is correct |