#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 1e5 + 5;
char ans[N];
bool f[N], brd[N];
int n, m, q, timer, depth, ts;
int a[N], b[N], disc[N], low[N], p[N], sz[N], dep[N], in[N], out[N], W[N][4], Pr[N][20];
vector < int > bridges;
vector < pair < int , int > > v[N], g[N];
void dfs(int x, int p) {
f[x] = true;
disc[x] = low[x] = ++timer;
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i].f, ed = v[x][i].s;
if (to == p) continue;
if (!f[to]) {
dfs(to, x);
low[x] = min(low[x], low[to]);
if (low[to] > disc[x]) {
brd[ed] = true;
bridges.pb(ed);
}
}
else {
low[x] = min(low[x], disc[to]);
}
}
}
int P(int x) {
if (p[x] == x) return x;
return p[x] = P(p[x]);
}
bool is_anc(int a, int b) {
return in[a] <= in[b] && out[b] <= out[a];
}
int lca(int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
if (is_anc(a, b)) return a;
for (int i = 17; i >= 0; --i)
if (!is_anc(Pr[a][i], b)) a = Pr[a][i];
return Pr[a][0];
}
void wfs(int x, int p) {
f[x] = true;
in[x] = ++ts;
Pr[x][0] = p;
for (int j = 1; j < 18; ++j) {
Pr[x][j] = Pr[Pr[x][j - 1]][j - 1];
}
for (int i = 0; i < g[x].size(); ++i) {
int to = g[x][i].f, ed = g[x][i].s;
if (to == p) continue;
dep[to] = dep[x] + 1;
wfs(to, x);
}
out[x] = ts;
}
void ufs(int x, int edg) {
for (int i = 0; i < g[x].size(); ++i) {
int to = g[x][i].f, ed = g[x][i].s;
if (ed == edg) continue;
ufs(to, ed);
W[x][0] += W[to][0];
W[x][1] += W[to][1];
}
if (edg != -1){
int A = P(a[edg]), B = P(b[edg]);
if (W[x][0] > 0) {
if (A == Pr[x][0] && B == x) ans[edg] = 'L';
else
if (A == x && B == Pr[x][0]) ans[edg] = 'R';
}
else
if (W[x][1] > 0) {
if (A == Pr[x][0] && B == x) ans[edg] = 'R';
else
if (A == x && B == Pr[x][0]) ans[edg] = 'L';
}
}
}
main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
ans[i] = 'B';
cin >> a[i] >> b[i];
v[a[i]].pb({b[i], i});
v[b[i]].pb({a[i], i});
}
for (int i = 1; i <= n; ++i)
if (!f[i]) dfs(i, -1);
for (int i = 1; i <= n; ++i) {
p[i] = i, sz[i] = 1, f[i] = 0;
}
for (int i = 1; i <= m; ++i) {
if (brd[i]) continue;
int A = P(a[i]), B = P(b[i]);
if (A == B) continue;
if (sz[A] < sz[B]) swap(A, B);
sz[A] += sz[B];
p[B] = A;
}
vector < int > nd;
for (int i = 0; i < bridges.size(); ++i) {
int id = bridges[i];
int A = P(a[id]);
int B = P(b[id]);
g[A].pb({B, id});
g[B].pb({A, id});
if (!f[A]) nd.pb(A), f[A] = true;
if (!f[B]) nd.pb(B), f[B] = true;
}
for (int i = 0; i < nd.size(); ++i)
f[nd[i]] = false;
for (int i = 0; i < nd.size(); ++i)
if (!f[nd[i]]) wfs(nd[i], nd[i]);
cin >> q;
for (int i = 1; i <= q; ++i) {
int x, y, z;
cin >> x >> y;
x = P(x), y = P(y);
if (x == y) continue;
z = lca(x, y);
--W[z][0], ++W[x][0];
--W[z][1], ++W[y][1];
}
for (int i = 0; i < nd.size(); ++i)
f[nd[i]] = false;
for (int i = 0; i < nd.size(); ++i)
if (!f[nd[i]]) ufs(nd[i], -1);
for (int i = 1; i <= m; ++i)
cout << ans[i];
}
Compilation message
oneway.cpp: In function 'void dfs(int, int)':
oneway.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < v[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void wfs(int, int)':
oneway.cpp:62:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i < g[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
oneway.cpp:63:23: warning: unused variable 'ed' [-Wunused-variable]
63 | int to = g[x][i].f, ed = g[x][i].s;
| ^~
oneway.cpp: In function 'void ufs(int, int)':
oneway.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < g[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
oneway.cpp: At global scope:
oneway.cpp:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
96 | main () {
| ^~~~
oneway.cpp: In function 'int main()':
oneway.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i = 0; i < bridges.size(); ++i) {
| ~~^~~~~~~~~~~~~~~~
oneway.cpp:134:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for (int i = 0; i < nd.size(); ++i)
| ~~^~~~~~~~~~~
oneway.cpp:137:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for (int i = 0; i < nd.size(); ++i)
| ~~^~~~~~~~~~~
oneway.cpp:151:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
151 | for (int i = 0; i < nd.size(); ++i)
| ~~^~~~~~~~~~~
oneway.cpp:154:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for (int i = 0; i < nd.size(); ++i)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |