#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int P = 20;
int n, m;
pair<int,int> edge[N];
vector<pair<int,int>> g[N];
int tin[N], tout[N], low[N];
bool vis[N], bridge[N];
int comp[N];
vector<pair<int,int>> gc[N];
int depth[N];
pair<int,int> parent[N];
int up[N][P+3];
int path[N][3];
int direction[N];
char label[N];
///drewo dwuspojnych
int timer = 0;
void dfs_low(int v, int p){
vis[v] = true;
tin[v] = low[v] = ++timer;
for (auto u : g[v]){
if (u.se == p) continue;
if (vis[u.fi]){
low[v] = min(low[v], tin[u.fi]);
}else{
dfs_low(u.fi, u.se);
low[v] = min(low[v], low[u.fi]);
if (low[u.fi] > tin[v]) bridge[u.se] = true;
}
}
}
void components(int v, int c){
vis[v] = true;
comp[v] = c;
for (auto u : g[v]){
if (!vis[u.fi] && !bridge[u.se]){
components(u.fi, c);
}
}
}
///LCA
int order = 0;
void dfs_lca(int v, int p, int idx){
vis[v] = true;
tin[v] = ++order;
up[v][0] = p;
parent[v] = {p, idx};
for (int i=1; i<=P; i++){
up[v][i] = up[up[v][i-1]][i-1];
}
for (auto u : gc[v]){
if (u.fi != p){
depth[u.fi] = depth[v] + 1;
dfs_lca(u.fi, v, u.se);
}
}
tout[v] = order;
}
bool is_ancestor(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i=P; i>=0; i--){
if (!is_ancestor(up[u][i], v)) u = up[u][i];
}
return up[u][0];
}
///skierowanie krawedzi
void direct(int x, int z, int dir){
if (x == z) return;
if (direction[x] == 0){
direction[x] = dir;
int p = parent[x].fi;
int idx = parent[x].se;
int a = comp[edge[idx].fi];
int b = comp[edge[idx].se];
if (dir == -1){
if (a == x && b == p) label[idx] = 'R';
else label[idx] = 'L';
}else{
if (a == x && b == p) label[idx] = 'L';
else label[idx] = 'R';
}
direct(p, z, dir);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i=0; i<m; i++){
int a, b; cin >> a >> b;
edge[i] = {a, b};
g[a].push_back({b, i});
g[b].push_back({a, i});
}
//drzewo dwuspojnych
for (int i=1; i<=n; i++){
if (!vis[i]) dfs_low(i, -1);
}
for (int i=1; i<=n; i++) vis[i] = false;
int c = 1;
for (int i=1; i<=n; i++){
if (!vis[i]){
components(i, c);
c++;
}
}
for (int i=0; i<m; i++){
if (bridge[i]){
int ca = comp[edge[i].fi];
int cb = comp[edge[i].se];
gc[ca].push_back({cb, i});
gc[cb].push_back({ca, i});
}
}
/*
cout << '\n';
for (int i=1; i<=n; i++){
cout << i << ": ";
for (auto ele : gc[i]) cout << ele.fi << ' ';
cout << '\n';
}
*/
//lca
for (int i=1; i<=n; i++) vis[i] = false;
for (int i=1; i<=n; i++){
if (!vis[i]){
depth[i] = 1;
dfs_lca(i, i, 0);
}
}
//skierowanie krawedzi
for (int i=0; i<m; i++) label[i] = 'B';
vector<pair<int,int>> ord;
int q; cin >> q;
for (int i=0; i<q; i++){
int a, b; cin >> a >> b;
path[i][0] = comp[a];
path[i][1] = comp[b];
path[i][2] = lca(comp[a], comp[b]);
ord.push_back({depth[path[i][2]], i});
}
sort(ord.begin(), ord.end());
for (auto ele : ord){
int a = path[ele.se][0];
int b = path[ele.se][1];
int lc = path[ele.se][2];
direct(a, lc, -1);
direct(b, lc, 1);
}
for (int i=0; i<m; i++) cout << label[i];
cout << '\n';
return 0;
}
/*
10 10
7 6
4 1
6 6
10 10
6 6
4 1
1 7
7 6
6 8
8 10
0
5 6
1 2
1 2
4 3
2 3
1 3
5 1
2
4 5
1 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
29 ms |
18268 KB |
Output is correct |
12 |
Correct |
34 ms |
21076 KB |
Output is correct |
13 |
Correct |
39 ms |
22100 KB |
Output is correct |
14 |
Correct |
49 ms |
27100 KB |
Output is correct |
15 |
Correct |
52 ms |
27340 KB |
Output is correct |
16 |
Correct |
66 ms |
27984 KB |
Output is correct |
17 |
Correct |
78 ms |
29520 KB |
Output is correct |
18 |
Correct |
80 ms |
28068 KB |
Output is correct |
19 |
Correct |
75 ms |
30548 KB |
Output is correct |
20 |
Correct |
35 ms |
20564 KB |
Output is correct |
21 |
Correct |
34 ms |
20308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
29 ms |
18268 KB |
Output is correct |
12 |
Correct |
34 ms |
21076 KB |
Output is correct |
13 |
Correct |
39 ms |
22100 KB |
Output is correct |
14 |
Correct |
49 ms |
27100 KB |
Output is correct |
15 |
Correct |
52 ms |
27340 KB |
Output is correct |
16 |
Correct |
66 ms |
27984 KB |
Output is correct |
17 |
Correct |
78 ms |
29520 KB |
Output is correct |
18 |
Correct |
80 ms |
28068 KB |
Output is correct |
19 |
Correct |
75 ms |
30548 KB |
Output is correct |
20 |
Correct |
35 ms |
20564 KB |
Output is correct |
21 |
Correct |
34 ms |
20308 KB |
Output is correct |
22 |
Correct |
127 ms |
31496 KB |
Output is correct |
23 |
Correct |
125 ms |
30188 KB |
Output is correct |
24 |
Correct |
110 ms |
30140 KB |
Output is correct |
25 |
Correct |
115 ms |
34488 KB |
Output is correct |
26 |
Correct |
114 ms |
31108 KB |
Output is correct |
27 |
Correct |
117 ms |
30152 KB |
Output is correct |
28 |
Correct |
27 ms |
15600 KB |
Output is correct |
29 |
Correct |
59 ms |
22080 KB |
Output is correct |
30 |
Correct |
61 ms |
22232 KB |
Output is correct |
31 |
Correct |
53 ms |
22496 KB |
Output is correct |
32 |
Correct |
72 ms |
27340 KB |
Output is correct |