#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
struct Edge {
int a, b;
int other(int node) {
return a ^ b ^ node;
}
}e[MAX_N + 5];
vector<int>G[MAX_N + 5];
int h[MAX_N + 5], low[MAX_N + 5];
bool viz[MAX_N + 5];
int t1[MAX_N + 5], h1[MAX_N + 5];
int indcomp[MAX_N + 5];
vector<int>comp[MAX_N + 5];
vector<int>G1[MAX_N + 5];
int t2[MAX_N + 5], h2[MAX_N + 5];
int niv[MAX_N + 5], f[MAX_N + 5];
int mp[MAX_N + 5], nd[MAX_N + 5];
char sol[MAX_N + 5];
int f1(int x) {
int y = x;
while (x != t1[x])
x = t1[x];
while (y != t1[y]) {
int aux = t1[y];
t1[y] = x;
y = aux;
}
return x;
}
void u1(int x, int y) {
x = f1(x);
y = f1(y);
if (x == y)
return ;
if (h1[y] > h1[x])
swap(x, y);
t1[y] = x;
if (h1[x] == h1[y])
h1[x]++;
}
void dfs(int nod, int ff) {
viz[nod] = 1;
for (auto it:G[nod]) {
int v = e[it].other(nod);
if (!viz[v]) {
low[v] = h[v] = h[nod] + 1;
dfs(v, it);
low[nod] = min(low[nod], low[v]);
if (low[v] <= h[nod])
u1(nod, v);
} else if (it != ff) {
low[nod] = min(low[nod], h[v]);
}
}
}
void solve1(int nod, int ff) {
viz[nod] = 1;
for (auto it:G[nod])
if (it != ff) {
int v = e[it].other(nod);
if (indcomp[nod] == indcomp[v]) {
sol[it] = 'B';
if (!viz[v])
solve1(v, it);
}
}
}
int other(int it, int nod) {
if (indcomp[e[it].a] == nod)
return e[it].b;
return e[it].a;
}
void dfs2(int nod, int ff) {
viz[nod] = 1;
for (auto it:G1[nod]) {
int v = indcomp[other(it, nod)];
if (v != ff) {
niv[v] = 1 + niv[nod];
f[v] = nod;
mp[v] = it;
dfs2(v, nod);
}
}
}
int f2(int x) {
int y = x;
while (t2[x] != x)
x = t2[x];
while (y != t2[y]) {
int aux = t2[y];
t2[y] = x;
niv[y] = niv[x];
nd[y] = nd[x];
y = aux;
}
return x;
}
void u2(int x, int y) {
x = f2(x);
y = f2(y);
if (x == y)
return ;
if (h[y] > h[x])
swap(x, y);
t2[y] = x;
if (h2[x] == h2[y])
h2[x]++;
if (niv[y] < niv[x]) {
niv[x] = niv[y];
nd[x] = nd[y];
}
}
void solve2(int a, int b) {
if (sol[mp[a]])
a = nd[f2(a)];
if (sol[mp[b]])
b = nd[f2(b)];
while (a != b) {
if (niv[a] > niv[b]) {
int aux = f[a];
if (indcomp[e[mp[a]].a] == a)
sol[mp[a]] = 'R';
else
sol[mp[a]] = 'L';
u2(aux, a);
if (sol[mp[f[a]]]) {
u2(a, f[a]);
a = nd[f2(a)];
} else {
a = aux;
}
} else {
int aux = f[b];
if (indcomp[e[mp[b]].a] == b)
sol[mp[b]] = 'L';
else
sol[mp[b]] = 'R';
u2(aux, b);
if (sol[mp[f[b]]]) {
u2(b, f[b]);
b = nd[f2(b)];
} else {
b = aux;
}
}
}
}
int main() {
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
e[i] = {x, y};
G[x].push_back(i);
G[y].push_back(i);
}
for (int i = 1; i <= n; ++i)
t1[i] = t2[i] = nd[i] = i;
for (int i = 1; i <= n; ++i)
if (!viz[i])
dfs(i, 0);
for (int i = 1; i <= n; ++i) {
int aux = f1(i);
indcomp[i] = aux;
comp[aux].push_back(i);
}
for (int i = 1; i <= n; ++i)
if ((int)comp[i].size() > 0) {
memset(viz, 0, sizeof(viz));
solve1(comp[i][0], 0);
}
for (int i = 1; i <= m; ++i)
if (indcomp[e[i].a] != indcomp[e[i].b]) {
G1[indcomp[e[i].a]].push_back(i);
G1[indcomp[e[i].b]].push_back(i);
}
memset(viz, 0, sizeof(viz));
for (int i = 1; i <= n; ++i)
if (!viz[indcomp[i]])
dfs2(indcomp[i], 0);
int p;
scanf("%d", &p);
for (int i = 1; i <= p; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if (indcomp[x] != indcomp[y])
solve2(indcomp[x], indcomp[y]);
}
for (int i = 1; i <= m; ++i) {
if (!sol[i])
sol[i] = 'B';
printf("%c", sol[i]);
}
return 0;
}
Compilation message
oneway.cpp: In function 'int main()':
oneway.cpp:172:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
oneway.cpp:175:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
oneway.cpp:212:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &p);
~~~~~^~~~~~~~~~
oneway.cpp:215:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7616 KB |
Output is correct |
3 |
Correct |
11 ms |
7724 KB |
Output is correct |
4 |
Correct |
18 ms |
7756 KB |
Output is correct |
5 |
Correct |
16 ms |
7908 KB |
Output is correct |
6 |
Correct |
9 ms |
7908 KB |
Output is correct |
7 |
Correct |
16 ms |
7908 KB |
Output is correct |
8 |
Correct |
15 ms |
7908 KB |
Output is correct |
9 |
Correct |
9 ms |
7964 KB |
Output is correct |
10 |
Correct |
11 ms |
7980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7616 KB |
Output is correct |
3 |
Correct |
11 ms |
7724 KB |
Output is correct |
4 |
Correct |
18 ms |
7756 KB |
Output is correct |
5 |
Correct |
16 ms |
7908 KB |
Output is correct |
6 |
Correct |
9 ms |
7908 KB |
Output is correct |
7 |
Correct |
16 ms |
7908 KB |
Output is correct |
8 |
Correct |
15 ms |
7908 KB |
Output is correct |
9 |
Correct |
9 ms |
7964 KB |
Output is correct |
10 |
Correct |
11 ms |
7980 KB |
Output is correct |
11 |
Correct |
86 ms |
13428 KB |
Output is correct |
12 |
Correct |
88 ms |
16036 KB |
Output is correct |
13 |
Correct |
156 ms |
18928 KB |
Output is correct |
14 |
Correct |
307 ms |
23120 KB |
Output is correct |
15 |
Correct |
443 ms |
25564 KB |
Output is correct |
16 |
Correct |
660 ms |
28904 KB |
Output is correct |
17 |
Correct |
657 ms |
31628 KB |
Output is correct |
18 |
Correct |
704 ms |
31628 KB |
Output is correct |
19 |
Correct |
692 ms |
35088 KB |
Output is correct |
20 |
Correct |
107 ms |
35088 KB |
Output is correct |
21 |
Correct |
101 ms |
35088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7616 KB |
Output is correct |
3 |
Correct |
11 ms |
7724 KB |
Output is correct |
4 |
Correct |
18 ms |
7756 KB |
Output is correct |
5 |
Correct |
16 ms |
7908 KB |
Output is correct |
6 |
Correct |
9 ms |
7908 KB |
Output is correct |
7 |
Correct |
16 ms |
7908 KB |
Output is correct |
8 |
Correct |
15 ms |
7908 KB |
Output is correct |
9 |
Correct |
9 ms |
7964 KB |
Output is correct |
10 |
Correct |
11 ms |
7980 KB |
Output is correct |
11 |
Correct |
86 ms |
13428 KB |
Output is correct |
12 |
Correct |
88 ms |
16036 KB |
Output is correct |
13 |
Correct |
156 ms |
18928 KB |
Output is correct |
14 |
Correct |
307 ms |
23120 KB |
Output is correct |
15 |
Correct |
443 ms |
25564 KB |
Output is correct |
16 |
Correct |
660 ms |
28904 KB |
Output is correct |
17 |
Correct |
657 ms |
31628 KB |
Output is correct |
18 |
Correct |
704 ms |
31628 KB |
Output is correct |
19 |
Correct |
692 ms |
35088 KB |
Output is correct |
20 |
Correct |
107 ms |
35088 KB |
Output is correct |
21 |
Correct |
101 ms |
35088 KB |
Output is correct |
22 |
Correct |
763 ms |
38668 KB |
Output is correct |
23 |
Correct |
719 ms |
39404 KB |
Output is correct |
24 |
Correct |
798 ms |
41808 KB |
Output is correct |
25 |
Correct |
754 ms |
48636 KB |
Output is correct |
26 |
Correct |
760 ms |
48636 KB |
Output is correct |
27 |
Correct |
768 ms |
48908 KB |
Output is correct |
28 |
Correct |
76 ms |
48908 KB |
Output is correct |
29 |
Correct |
136 ms |
48908 KB |
Output is correct |
30 |
Correct |
127 ms |
48908 KB |
Output is correct |
31 |
Correct |
116 ms |
48908 KB |
Output is correct |
32 |
Correct |
295 ms |
55052 KB |
Output is correct |