#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
typedef long long ll;
typedef pair <int, int> ii;
const int N = 1e5 + 1;
const int logN = 17;
struct edge{
int u, v;
edge(int _u = 0, int _v = 0){
u = _u;
v = _v;
}
};
struct dsu{
int root[N], rnk[N];
void reset(int n){
for(int i = 1; i <= n; i++){
root[i] = i;
rnk[i] = 0;
}
}
int getRoot(int u){
if (u == root[u])
return u;
root[u] = getRoot(root[u]);
return root[u];
}
bool unite(int u, int v){
u = getRoot(u);
v = getRoot(v);
if (u == v)
return 0;
if (rnk[u] < rnk[v])
swap(u, v);
root[v] = u;
rnk[u] += (rnk[u] == rnk[v]);
return 1;
}
} G;
int n, m, p;
edge e[N];
bool used[N];
vector <ii> adj[N];
int h[N], par[N][logN], edgeToPar[N], cnt[N], dir[N];
char ans[N];
int inComp[N], nComp;
void readInput(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++)
scanf("%d %d", &e[i].u, &e[i].v);
}
void dfs(int u, int p){
inComp[u] = nComp;
for(int i = 1; i < logN; i++)
par[u][i] = par[par[u][i - 1]][i - 1];
for(ii e : adj[u]){
int v = e.X;
int id = e.Y;
if (v != p){
h[v] = h[u] + 1;
par[v][0] = u;
edgeToPar[v] = id;
dfs(v, u);
}
}
}
int getLCA(int u, int v){
if (h[u] > h[v])
swap(u, v);
for(int i = logN - 1; i >= 0; i--)
if (h[u] <= h[v] - (1 << i))
v = par[v][i];
if (u == v)
return u;
for(int i = logN - 1; i >= 0; i--)
if (par[u][i] != par[v][i]){
u = par[u][i];
v = par[v][i];
}
return par[u][0];
}
void slowMarkNotCutEdge(int u, int v){
while (u != v){
if (h[u] > h[v])
swap(u, v);
cnt[v]++;
v = par[v][0];
}
}
void slowMarkDir(int u, int v){
while (u != v){
if (h[u] > h[v]){
dir[u]--;
u = par[u][0];
} else {
dir[v]++;
v = par[v][0];
}
}
}
void markNotCutEdge(int u, int v){
int lca = getLCA(u, v);
cnt[u]++;
cnt[v]++;
cnt[lca] -= 2;
}
void markDir(int u, int v){
dir[u]--;
dir[v]++;
}
void dfs2(int u, int p){
for(ii e : adj[u]){
int v = e.X;
if (v != p){
dfs2(v, u);
cnt[u] += cnt[v];
dir[u] += dir[v];
}
}
}
void prepare(){
G.reset(n);
for(int i = 1; i <= m; i++)
if (G.unite(e[i].u, e[i].v)){
used[i] = 1;
adj[e[i].u].push_back(mp(e[i].v, i));
adj[e[i].v].push_back(mp(e[i].u, i));
}
for(int i = 1; i <= n; i++)
if (!par[i][0]){
++nComp;
dfs(i, -1);
}
for(int i = 1; i <= m; i++)
if (!used[i])
markNotCutEdge(e[i].u, e[i].v);
}
void solve(){
scanf("%d", &p);
for(int i = 1; i <= p; i++){
int u, v;
scanf("%d %d", &u, &v);
markDir(u, v);
}
memset(ans, 'B', sizeof(ans));
for(int i = 1; i <= n; i++)
if (par[i][0] == 0)
dfs2(i, -1);
for(int i = 1; i <= n; i++)
if (par[i][0]){
if (cnt[i] > 0 || dir[i] == 0)
continue;
int id = edgeToPar[i];
if (dir[i] < 0)
ans[id] = (e[id].u == i? 'R' : 'L');
else
ans[id] = (e[id].u == i? 'L' : 'R');
}
for(int i = 1; i <= m; i++)
putchar(ans[i]);
}
int main(){
readInput();
prepare();
solve();
return 0;
}
Compilation message
oneway.cpp: In function 'void readInput()':
oneway.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
62 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
oneway.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf("%d %d", &e[i].u, &e[i].v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp: In function 'void solve()':
oneway.cpp:164:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
164 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
oneway.cpp:167:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
167 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3564 KB |
Output is correct |
2 |
Correct |
3 ms |
3564 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3820 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3820 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
3 ms |
3692 KB |
Output is correct |
10 |
Correct |
3 ms |
3692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3564 KB |
Output is correct |
2 |
Correct |
3 ms |
3564 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3820 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3820 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
3 ms |
3692 KB |
Output is correct |
10 |
Correct |
3 ms |
3692 KB |
Output is correct |
11 |
Correct |
56 ms |
7268 KB |
Output is correct |
12 |
Correct |
55 ms |
8804 KB |
Output is correct |
13 |
Correct |
68 ms |
11364 KB |
Output is correct |
14 |
Correct |
91 ms |
15072 KB |
Output is correct |
15 |
Correct |
98 ms |
16228 KB |
Output is correct |
16 |
Correct |
103 ms |
18020 KB |
Output is correct |
17 |
Correct |
98 ms |
19560 KB |
Output is correct |
18 |
Correct |
103 ms |
18008 KB |
Output is correct |
19 |
Correct |
98 ms |
20708 KB |
Output is correct |
20 |
Correct |
74 ms |
11492 KB |
Output is correct |
21 |
Correct |
57 ms |
11748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3564 KB |
Output is correct |
2 |
Correct |
3 ms |
3564 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3820 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3820 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
3 ms |
3692 KB |
Output is correct |
10 |
Correct |
3 ms |
3692 KB |
Output is correct |
11 |
Correct |
56 ms |
7268 KB |
Output is correct |
12 |
Correct |
55 ms |
8804 KB |
Output is correct |
13 |
Correct |
68 ms |
11364 KB |
Output is correct |
14 |
Correct |
91 ms |
15072 KB |
Output is correct |
15 |
Correct |
98 ms |
16228 KB |
Output is correct |
16 |
Correct |
103 ms |
18020 KB |
Output is correct |
17 |
Correct |
98 ms |
19560 KB |
Output is correct |
18 |
Correct |
103 ms |
18008 KB |
Output is correct |
19 |
Correct |
98 ms |
20708 KB |
Output is correct |
20 |
Correct |
74 ms |
11492 KB |
Output is correct |
21 |
Correct |
57 ms |
11748 KB |
Output is correct |
22 |
Correct |
121 ms |
20708 KB |
Output is correct |
23 |
Correct |
118 ms |
19172 KB |
Output is correct |
24 |
Correct |
121 ms |
19172 KB |
Output is correct |
25 |
Correct |
126 ms |
23780 KB |
Output is correct |
26 |
Correct |
125 ms |
20324 KB |
Output is correct |
27 |
Correct |
116 ms |
19300 KB |
Output is correct |
28 |
Correct |
45 ms |
4836 KB |
Output is correct |
29 |
Correct |
77 ms |
12768 KB |
Output is correct |
30 |
Correct |
75 ms |
12648 KB |
Output is correct |
31 |
Correct |
99 ms |
12620 KB |
Output is correct |
32 |
Correct |
101 ms |
17124 KB |
Output is correct |