/* * * * * * * * * * * * * * * * *
* author: enmendurana
* created: 09-14-2023
* * * * * * * * * * * * * * * * */
#include <bits/stdc++.h>
using namespace std;
struct buffer : streambuf {
int overflow(int x) {
return x;
}
};
static buffer null_buffer;
static ostream null_stream(&null_buffer);
void local() {
#if defined(LOCAL_FLAG)
freopen("/home/omega/Documents/input.in", "r", stdin);
freopen("/home/omega/Documents/output.out", "w", stdout);
freopen("/home/omega/Documents/error.err", "w", stderr);
#define debug cerr
#else
#define debug null_stream
#endif
}
#define ff first
#define ss second
#define pb emplace_back
#define pob pop_back
#define pf emplace_front
#define pof pop_front
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef pair<ll, ll> pll;
typedef tuple<ll,ll,ll> tlll;
const ll mod = ll(1e9 + 7);
const int N = 10'000;
const int maxn = int(1e5)+10;
const int lg = 21;
const double eps = double(1e-9);
/* ALGORITHM */
vector<vector<int>> g;
int edge[maxn], smer[maxn];
int d[maxn], low[maxn], timer = 0;
int p[maxn], cycle[maxn];
int aux[maxn], sz[maxn];
stack<int> st;
int idx[maxn];
int t[4*maxn];
int tarjan_dfs(int prev, int v) {
d[v] = low[v] = ++timer;
p[v] = edge[prev] - v;
sz[v] = 1;
st.emplace(v);
for(int i : g[v]) {
int u = edge[i] - v;
if(!d[u]) {
d[u] = d[v] + 1;
sz[v] += tarjan_dfs(i,u);
low[v] = min(low[v], low[u]);
} else if(i != prev) {
low[v] = min(low[v], d[u]);
}
}
if(d[v] == low[v]) {
for(int u = 0; u != v;) {
u = st.top(); st.pop();
cycle[u] = v;
}
}
return sz[v];
}
void hld(int r, int v) {
if(aux[v]) return;
priority_queue<ii> pq;
aux[v] = r;
idx[v] = timer++;
for(int i : g[v]) {
int u = edge[i] - v;
if(p[u] == v && u != v) {
pq.emplace(sz[u], u);
}
}
if(!pq.empty()) {
auto [w,u] = pq.top(); pq.pop();
hld(r,u);
}
while(!pq.empty()) {
auto [w,u] = pq.top(); pq.pop();
hld(u,u);
}
}
void update(int v, int l, int r, int lx, int rx, int x) {
if(lx <= l && r <= rx) t[v] = x;
else if(r <= lx || rx <= l) return;
else {
int mid = (l+r)>>1;
update(2*v,l,mid,lx,rx, x);
update(2*v+1,mid,r,lx,rx, x);
}
}
void lca(int v, int u) {
while(aux[v] != aux[u]) {
if(d[aux[v]] < d[aux[u]]) {
update(1,0,2*timer,idx[aux[u]],idx[u]+1,-1);
u = p[aux[u]];
} else {
update(1,0,2*timer,idx[aux[v]],idx[v]+1,1);
v = p[aux[v]];
}
}
if(d[v] < d[u]) update(1,0,2*timer,idx[v]+1,idx[u]+1,-1);
if(d[v] > d[u]) update(1,0,2*timer,idx[u]+1,idx[v]+1,1);
}
int mode[maxn];
void build(int v, int l, int r) {
if(r-l==1) {
if(l < timer) mode[l] = t[v];
} else {
if(t[v] != 0) t[2*v] = t[2*v+1] = t[v];
int mid = (l+r)>>1;
build(2*v,l,mid);
build(2*v+1,mid,r);
}
}
void solve() {
int n, m; cin >> n >> m;
g.resize(n+1);
for(int v, u, i = 0; i < m; i++) {
cin >> v >> u;
edge[i] = v + u;
smer[i] = (v < u);
g[v].pb(i);
g[u].pb(i);
}
for(int v = 1; v <= n; v++) {
if(!d[v]) {
edge[m] = 2*v;
tarjan_dfs(m,v);
}
}
timer = 0;
for(int v = 1; v <= n; v++) {
if(v==p[v]) hld(v,v);
}
int k; cin >> k;
for(int v, u; k--;) {
cin >> v >> u; lca(v,u);
}
build(1,0,2*timer);
string ans; ans.resize(m);
for(int v = 1; v <= n; v++) {
for(int i : g[v]) {
int u = edge[i] - v;
int op = mode[idx[u]];
if(cycle[v] == cycle[u]) {
ans[i] = 'B'; continue;
}
if(p[u] == v) {
if(op==1) {
if((u < v) ^ smer[i]) op = -1;
else op = 1;
} else if(op==-1) {
if((v < u) ^ smer[i]) op = -1;
else op = 1;
}
if(op==1) ans[i] = 'R';
if(op==-1) ans[i] = 'L';
if(op==0) ans[i] = 'B';
}
}
}
cout << ans << "\n";
}
signed main() {
local();
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1; // cin>>t;
while(t--) {
solve();
}
return 0;
}
// 想上GM捏 想上GM捏 想上GM捏 想上GM捏 想上GM捏
// 伊娜可爱捏 伊娜贴贴捏
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
27 ms |
10576 KB |
Output is correct |
12 |
Correct |
31 ms |
12112 KB |
Output is correct |
13 |
Correct |
39 ms |
14164 KB |
Output is correct |
14 |
Correct |
62 ms |
15636 KB |
Output is correct |
15 |
Correct |
48 ms |
15836 KB |
Output is correct |
16 |
Correct |
37 ms |
12836 KB |
Output is correct |
17 |
Correct |
43 ms |
15704 KB |
Output is correct |
18 |
Correct |
40 ms |
12892 KB |
Output is correct |
19 |
Correct |
52 ms |
17744 KB |
Output is correct |
20 |
Correct |
32 ms |
12376 KB |
Output is correct |
21 |
Correct |
31 ms |
11860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
27 ms |
10576 KB |
Output is correct |
12 |
Correct |
31 ms |
12112 KB |
Output is correct |
13 |
Correct |
39 ms |
14164 KB |
Output is correct |
14 |
Correct |
62 ms |
15636 KB |
Output is correct |
15 |
Correct |
48 ms |
15836 KB |
Output is correct |
16 |
Correct |
37 ms |
12836 KB |
Output is correct |
17 |
Correct |
43 ms |
15704 KB |
Output is correct |
18 |
Correct |
40 ms |
12892 KB |
Output is correct |
19 |
Correct |
52 ms |
17744 KB |
Output is correct |
20 |
Correct |
32 ms |
12376 KB |
Output is correct |
21 |
Correct |
31 ms |
11860 KB |
Output is correct |
22 |
Correct |
133 ms |
16916 KB |
Output is correct |
23 |
Correct |
171 ms |
14036 KB |
Output is correct |
24 |
Correct |
172 ms |
13908 KB |
Output is correct |
25 |
Correct |
114 ms |
22296 KB |
Output is correct |
26 |
Correct |
145 ms |
16188 KB |
Output is correct |
27 |
Correct |
152 ms |
14136 KB |
Output is correct |
28 |
Correct |
29 ms |
7760 KB |
Output is correct |
29 |
Correct |
132 ms |
12616 KB |
Output is correct |
30 |
Correct |
105 ms |
12880 KB |
Output is correct |
31 |
Correct |
99 ms |
13496 KB |
Output is correct |
32 |
Correct |
92 ms |
17232 KB |
Output is correct |