#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define LOGN 20
const ll MOD = 1e9 + 7;
const ll MAXN = 1e5 + 100;
vector<vector<int>> graph, G, G_rev;
vector<vector<pair<int,int>>> G2;
int mark[MAXN], a[MAXN], b[MAXN], vis[MAXN], par[MAXN], sz[MAXN], ways[MAXN];
int find(int node) {
if (node == par[node])
return node;
return par[node] = find(par[node]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return ;
if (sz[b] > sz[a])
swap(a, b);
par[b] = a;
sz[a] += sz[b];
}
void dfs(int node, int parent) {
vis[node] = 1;
for (auto u : graph[node]) {
if (u != parent && vis[u] != 2) {
G[node].push_back(u);
G_rev[u].push_back(node);
}
if (!vis[u])
dfs(u, node);
}
vis[node] = 2;
}
stack<int> nds;
void dfs2(int node) {
vis[node] = true;
for (auto u : G[node]) {
if (!vis[u])
dfs2(u);
}
nds.push(node);
}
void dfs3(int node) {
vis[node] = true;
for (auto u : G_rev[node]) {
if (vis[u])
continue;
unite(node, u);
dfs3(u);
}
}
void dfs4(int node) {
vis[node] = true;
for (auto u : G2[node]) {
if (vis[u.f])
continue;
dfs4(u.f);
mark[node] += mark[u.f];
if (find(a[u.s]) == node) {
if (mark[u.f] > 0)
ways[u.s] = 1;
else if (mark[u.f] < 0)
ways[u.s] = 2;
} else { // b[u.s] == node
if (mark[u.f] > 0)
ways[u.s] = 2;
else if (mark[u.f] < 0)
ways[u.s] = 1;
}
}
}
map<pair<int,int>,int> mp;
int main() {
fast
int n, m, q;
cin >> n >> m;
for (int i = 1; i <= n; i++)
par[i] = i, sz[i] = 1;
graph = vector<vector<int>>(n+1, vector<int>());
G = vector<vector<int>>(n+1, vector<int>());
G2 = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
G_rev = vector<vector<int>>(n+1, vector<int>());
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
mp[{min(a[i], b[i]), max(a[i], b[i])}]++;
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
for (int i = 1; i <= n; i++) {
if (!vis[i])
dfs(i, i);
}
for (int i = 1; i <= n; i++)
vis[i] = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i])
dfs2(i);
}
for (int i = 1; i <= n; i++)
vis[i] = 0;
while (nds.size()) {
int node = nds.top();
nds.pop();
if (!vis[node])
dfs3(node);
}
for (int i = 1; i <= m; i++) {
if (find(a[i]) == find(b[i]))
ways[i] = -1;
else {
G2[find(a[i])].push_back({find(b[i]), i});
G2[find(b[i])].push_back({find(a[i]), i});
}
}
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
a = find(a);
b = find(b);
if (a == b)
continue;
mark[b] -= 1;
mark[a] += 1;
}
for (int i = 1; i <= n; i++)
vis[i] = false;
for (int i = 1; i <= n; i++) {
if (!vis[find(i)])
dfs4(find(i));
}
for (int i = 1; i <= m; i++) {
if (mp[{min(a[i], b[i]), max(a[i], b[i])}] > 1 || a[i] == b[i])
cout << "B";
else if (ways[i] == 1)
cout << "L";
else if (ways[i] == 2)
cout << "R";
else
cout << "B";
}
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
86 ms |
15444 KB |
Output is correct |
12 |
Correct |
93 ms |
17744 KB |
Output is correct |
13 |
Correct |
88 ms |
21460 KB |
Output is correct |
14 |
Correct |
103 ms |
26748 KB |
Output is correct |
15 |
Correct |
123 ms |
28112 KB |
Output is correct |
16 |
Correct |
120 ms |
30544 KB |
Output is correct |
17 |
Correct |
95 ms |
32848 KB |
Output is correct |
18 |
Correct |
112 ms |
30656 KB |
Output is correct |
19 |
Correct |
114 ms |
34124 KB |
Output is correct |
20 |
Correct |
84 ms |
20308 KB |
Output is correct |
21 |
Correct |
78 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
86 ms |
15444 KB |
Output is correct |
12 |
Correct |
93 ms |
17744 KB |
Output is correct |
13 |
Correct |
88 ms |
21460 KB |
Output is correct |
14 |
Correct |
103 ms |
26748 KB |
Output is correct |
15 |
Correct |
123 ms |
28112 KB |
Output is correct |
16 |
Correct |
120 ms |
30544 KB |
Output is correct |
17 |
Correct |
95 ms |
32848 KB |
Output is correct |
18 |
Correct |
112 ms |
30656 KB |
Output is correct |
19 |
Correct |
114 ms |
34124 KB |
Output is correct |
20 |
Correct |
84 ms |
20308 KB |
Output is correct |
21 |
Correct |
78 ms |
20052 KB |
Output is correct |
22 |
Correct |
112 ms |
32848 KB |
Output is correct |
23 |
Correct |
139 ms |
30800 KB |
Output is correct |
24 |
Correct |
111 ms |
30500 KB |
Output is correct |
25 |
Correct |
121 ms |
36692 KB |
Output is correct |
26 |
Correct |
110 ms |
32340 KB |
Output is correct |
27 |
Correct |
130 ms |
30988 KB |
Output is correct |
28 |
Correct |
35 ms |
5208 KB |
Output is correct |
29 |
Correct |
101 ms |
20156 KB |
Output is correct |
30 |
Correct |
86 ms |
20236 KB |
Output is correct |
31 |
Correct |
93 ms |
20340 KB |
Output is correct |
32 |
Correct |
93 ms |
24404 KB |
Output is correct |