#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
struct Edge {
int u, v;
bool isBridge, used;
Edge(int _u = 0, int _v = 0) {
u = _u; v = _v;
isBridge = used = 0;
}
} e[N];
int n, m, q;
int low[N], num[N];
vector<int> adj[N];
vector<ii> newAdj[N];
void read() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
e[i] = Edge(u, v);
adj[u].push_back(i);
adj[v].push_back(i);
}
}
int timer = 0, cycle = 0;
stack<int> st;
int id[N];
void tarjan(int u) {
low[u] = num[u] = ++timer;
st.push(u);
for(int i: adj[u]) if (!e[i].used) {
e[i].used = 1;
int v = e[i].u ^ e[i].v ^ u;
if (num[v]) low[u] = min(low[u], num[v]);
else {
tarjan(v);
low[u] = min(low[u], low[v]);
}
if (low[v] > num[u]) {
// cerr << "BRIDGE: " << e[i].u << ' ' << e[i].v << endl;
e[i].isBridge = 1;
}
}
if (low[u] == num[u]) {
++cycle;
// cerr << cycle << endl;
while(true) {
int v = st.top(); st.pop();
// cerr << v << ' ';
low[v] = num[v] = inf;
id[v] = cycle;
if (v == u) break;
}
// cerr << endl;
}
}
int tin[N], tout[N], high[N];
ii par[N];
bool isChild(int u, int p) {
return tin[u] >= tin[p] && tin[u] <= tout[p];
}
int jump[N];
void dfs(int u, int p) {
// cerr << u << endl;
tin[u] = ++timer;
jump[u] = u;
for(auto [v, i]: newAdj[u]) if (v != p) {
par[v] = {u, i};
high[v] = high[u] + 1;
dfs(v, u);
}
tout[u] = timer;
}
int ans[N];
int change[N];
vector<int> node;
void solve() {
for(int i = 1; i <= n; i++) if (!num[i]) {
tarjan(i);
// assert(i == 1);
}
for(int i = 1; i <= m; i++) if (e[i].isBridge) {
int u = id[e[i].u], v = id[e[i].v];
newAdj[u].push_back({v, i});
newAdj[v].push_back({u, i});
}
timer = 0;
for(int i = 1; i <= cycle; i++) if (!tin[i]) dfs(i, -1);
cin >> q;
while(q--) {
int u, v; cin >> u >> v;
// cerr << id[u] << ' ' << id[v] << endl;
if (id[u] == id[v]) continue;
u = id[u]; v = id[v];
int tmp = u;
// cerr << jump[u] << ' ' << v << ' ' << isChild(jump[u], v) << endl;
while(!isChild(v, jump[u])) {
int i = par[jump[u]].se;
// u = par[u].fi;
ans[i] = 1;
node.push_back(u);
u = par[jump[u]].fi;
}
for(int x: node) jump[x] = u;
node.clear();
u = tmp;
while(!isChild(u, jump[v])) {
int i = par[jump[v]].se;
ans[i] = 2;
// cerr << v << ' ' << e[i].u << ' ' << e[i].v << endl;
node.push_back(v);
v = par[jump[v]].fi;
}
for(int x: node) jump[x] = v;
node.clear();
}
for(int i = 1; i <= m; i++) {
// cout << e[i].u << ' ' << e[i].v << ' ';
if (e[i].isBridge) {
if (ans[i] != 0) {
int u = e[i].u, v = e[i].v;
if (high[id[u]] < high[id[v]]) {
assert(isChild(id[v], id[u]));
if (ans[i] == 2) cout << 'R';
else cout << 'L';
}
else {
assert(isChild(id[u], id[v]));
if (ans[i] == 2) cout << 'L';
else cout << 'R';
}
}
else cout << 'B';
// cout << endl;
continue;
}
else cout << 'B';
// cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
// rmq - rmq2d
// hash
// fw - fw2d
// segtree
Compilation message
oneway.cpp: In function 'int main()':
oneway.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6224 KB |
Output is correct |
2 |
Correct |
5 ms |
6236 KB |
Output is correct |
3 |
Correct |
7 ms |
6236 KB |
Output is correct |
4 |
Correct |
6 ms |
6488 KB |
Output is correct |
5 |
Correct |
6 ms |
6492 KB |
Output is correct |
6 |
Correct |
7 ms |
6236 KB |
Output is correct |
7 |
Correct |
6 ms |
6492 KB |
Output is correct |
8 |
Correct |
5 ms |
6480 KB |
Output is correct |
9 |
Correct |
6 ms |
6248 KB |
Output is correct |
10 |
Correct |
5 ms |
6224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6224 KB |
Output is correct |
2 |
Correct |
5 ms |
6236 KB |
Output is correct |
3 |
Correct |
7 ms |
6236 KB |
Output is correct |
4 |
Correct |
6 ms |
6488 KB |
Output is correct |
5 |
Correct |
6 ms |
6492 KB |
Output is correct |
6 |
Correct |
7 ms |
6236 KB |
Output is correct |
7 |
Correct |
6 ms |
6492 KB |
Output is correct |
8 |
Correct |
5 ms |
6480 KB |
Output is correct |
9 |
Correct |
6 ms |
6248 KB |
Output is correct |
10 |
Correct |
5 ms |
6224 KB |
Output is correct |
11 |
Correct |
37 ms |
9396 KB |
Output is correct |
12 |
Correct |
39 ms |
10312 KB |
Output is correct |
13 |
Correct |
38 ms |
11684 KB |
Output is correct |
14 |
Correct |
50 ms |
13640 KB |
Output is correct |
15 |
Correct |
48 ms |
14376 KB |
Output is correct |
16 |
Correct |
63 ms |
18076 KB |
Output is correct |
17 |
Correct |
61 ms |
19016 KB |
Output is correct |
18 |
Correct |
65 ms |
17312 KB |
Output is correct |
19 |
Correct |
55 ms |
19880 KB |
Output is correct |
20 |
Correct |
34 ms |
11212 KB |
Output is correct |
21 |
Correct |
37 ms |
10312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6224 KB |
Output is correct |
2 |
Correct |
5 ms |
6236 KB |
Output is correct |
3 |
Correct |
7 ms |
6236 KB |
Output is correct |
4 |
Correct |
6 ms |
6488 KB |
Output is correct |
5 |
Correct |
6 ms |
6492 KB |
Output is correct |
6 |
Correct |
7 ms |
6236 KB |
Output is correct |
7 |
Correct |
6 ms |
6492 KB |
Output is correct |
8 |
Correct |
5 ms |
6480 KB |
Output is correct |
9 |
Correct |
6 ms |
6248 KB |
Output is correct |
10 |
Correct |
5 ms |
6224 KB |
Output is correct |
11 |
Correct |
37 ms |
9396 KB |
Output is correct |
12 |
Correct |
39 ms |
10312 KB |
Output is correct |
13 |
Correct |
38 ms |
11684 KB |
Output is correct |
14 |
Correct |
50 ms |
13640 KB |
Output is correct |
15 |
Correct |
48 ms |
14376 KB |
Output is correct |
16 |
Correct |
63 ms |
18076 KB |
Output is correct |
17 |
Correct |
61 ms |
19016 KB |
Output is correct |
18 |
Correct |
65 ms |
17312 KB |
Output is correct |
19 |
Correct |
55 ms |
19880 KB |
Output is correct |
20 |
Correct |
34 ms |
11212 KB |
Output is correct |
21 |
Correct |
37 ms |
10312 KB |
Output is correct |
22 |
Correct |
68 ms |
19016 KB |
Output is correct |
23 |
Correct |
107 ms |
19316 KB |
Output is correct |
24 |
Correct |
89 ms |
19584 KB |
Output is correct |
25 |
Correct |
83 ms |
24384 KB |
Output is correct |
26 |
Correct |
86 ms |
20608 KB |
Output is correct |
27 |
Correct |
99 ms |
19368 KB |
Output is correct |
28 |
Correct |
35 ms |
8616 KB |
Output is correct |
29 |
Correct |
65 ms |
12272 KB |
Output is correct |
30 |
Correct |
62 ms |
12620 KB |
Output is correct |
31 |
Correct |
54 ms |
12716 KB |
Output is correct |
32 |
Correct |
89 ms |
16728 KB |
Output is correct |