// Link: https://cses.fi/problemset/task/1691
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lc (node<<1)+1
#define rc (node<<1)+2
#define endl '\n'
#define INF 1e9
const int max_n = 1e5+2;
int n, m, k;
pair<int, int> edges[max_n];
vector<int> adj[max_n];
int enter[max_n], low[max_n];
int t = 0;
int scc[max_n]; // stores the id of the strongly connected component
int id = 0;
stack<int> s;
vector<int> tree[max_n];
int dp[max_n];
void dfs(int node, int parent = -1){
enter[node] = low[node] = t ++;
s.push(node);
bool pp = 0;
for(int next : adj[node]){
if(next != parent || pp){
if(enter[next] == -1){
dfs(next, node);
low[node] = min(low[node], low[next]);
if(low[next] == enter[next]){ // no edge going up
while(true){
int a = s.top();
scc[a] = id;
s.pop();
if(a == next) break;
}
id ++;
}
}
else{
low[node] = min(low[node], enter[next]);
}
}
else{
pp = 1;
}
}
}
int depth[max_n];
void dfs2(int node, int parent = -1, int dep = 0){
depth[node] = dep;
for(int next: tree[node]){
if(next == parent) continue;
dfs2(next, node, dep + 1);
dp[node] += dp[next];
}
}
int main() {
cin >> n >> m;
for(int i = 0; i < m; i ++){
int a, b; cin >> a >> b; a --; b --;
adj[a].push_back(b); adj[b].push_back(a);
edges[i] = {a, b};
}
// find 2-edge-connected-components and create a tree out of it
// always direct the edges from top->bottom
fill(enter, enter + n, -1);
for(int i = 0; i < n; i ++){
if(enter[i] == -1){
dfs(i);
while(!s.empty()){
int a = s.top();
s.pop();
scc[a] = id;
}
id ++;
}
}
for(int i = 0; i < m; i ++){
int a = scc[edges[i].first], b = scc[edges[i].second];
if(a == b) continue;
tree[a].push_back(b);
tree[b].push_back(a);
}
// now we loop through the required passages and assign values
cin >> k;
for(int i = 0; i < k; i ++){
int a, b; cin >> a >> b; a --; b --;
a = scc[a]; b = scc[b];
dp[a]++; dp[b]--; // positive means going up, negative means going down
}
for(int i = 0; i < id; i ++){
if(!depth[i]) dfs2(i);
}
for(int i = 0; i < m; i ++){
int a = scc[edges[i].first], b = scc[edges[i].second];
if(a == b) cout << 'B';
else if(depth[b] > depth[a]){ // this means a is above b
if(dp[b] < 0){
cout << 'R';
}
else if(dp[b] == 0){
cout << 'B';
}
else if(dp[b] > 0){
cout << 'L';
}
}
else{
if(dp[a] < 0){
cout << 'L';
}
else if(dp[a] == 0){
cout << 'B';
}
else if(dp[a] > 0){
cout << 'R';
}
}
}
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5016 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5016 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
65 ms |
10228 KB |
Output is correct |
12 |
Correct |
68 ms |
11268 KB |
Output is correct |
13 |
Correct |
73 ms |
12648 KB |
Output is correct |
14 |
Correct |
86 ms |
14124 KB |
Output is correct |
15 |
Correct |
114 ms |
14668 KB |
Output is correct |
16 |
Correct |
104 ms |
15388 KB |
Output is correct |
17 |
Correct |
92 ms |
17300 KB |
Output is correct |
18 |
Correct |
124 ms |
15384 KB |
Output is correct |
19 |
Correct |
87 ms |
18608 KB |
Output is correct |
20 |
Correct |
72 ms |
11204 KB |
Output is correct |
21 |
Correct |
86 ms |
10848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5016 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
65 ms |
10228 KB |
Output is correct |
12 |
Correct |
68 ms |
11268 KB |
Output is correct |
13 |
Correct |
73 ms |
12648 KB |
Output is correct |
14 |
Correct |
86 ms |
14124 KB |
Output is correct |
15 |
Correct |
114 ms |
14668 KB |
Output is correct |
16 |
Correct |
104 ms |
15388 KB |
Output is correct |
17 |
Correct |
92 ms |
17300 KB |
Output is correct |
18 |
Correct |
124 ms |
15384 KB |
Output is correct |
19 |
Correct |
87 ms |
18608 KB |
Output is correct |
20 |
Correct |
72 ms |
11204 KB |
Output is correct |
21 |
Correct |
86 ms |
10848 KB |
Output is correct |
22 |
Correct |
148 ms |
18328 KB |
Output is correct |
23 |
Correct |
158 ms |
16444 KB |
Output is correct |
24 |
Correct |
153 ms |
16580 KB |
Output is correct |
25 |
Correct |
140 ms |
21928 KB |
Output is correct |
26 |
Correct |
141 ms |
17852 KB |
Output is correct |
27 |
Correct |
149 ms |
16636 KB |
Output is correct |
28 |
Correct |
70 ms |
8040 KB |
Output is correct |
29 |
Correct |
118 ms |
11768 KB |
Output is correct |
30 |
Correct |
115 ms |
11976 KB |
Output is correct |
31 |
Correct |
123 ms |
12380 KB |
Output is correct |
32 |
Correct |
132 ms |
15768 KB |
Output is correct |