#include<bits/stdc++.h>
using namespace std;
set<pair<int, int>> edges;
map<pair<int, int>, int> f;
map<pair<int, int>, pair<int, int>> edge_tree_to_graph;
vector<pair<int, int>> bridges;
vector<int> ad[100005], ad2[100005];
int low[100005], num[100005], c[100005], dp[100005], ans[100005], dfsc = 0, color = 0;
//0 = B; 1 = L; 2 = R;
bool visited[100005];
inline pair<int, int> edgeCorrection(pair<int, int> x)
{
if(edges.count(x) == 0) swap(x.first, x.second);
return x;
}
void dfs(int u, int par)
{
low[u] = num[u] = ++dfsc;
bool avoidpar = 1;
for(int v : ad[u]){
if(v != par || avoidpar == 0){
if(num[v] > 0) low[u] = min(low[u], num[v]);
else{
dfs(v, u);
low[u] = min(low[u], low[v]);
}
}
else avoidpar = 0;
}
}
void dfscolor(int u, int co)
{
//cout<<"A"<<u<<" "<<color<<endl;
visited[u] = 1; c[u] = co;
for(int v : ad[u]) if(visited[v] == 0){
if(low[v] == num[v]){
dfscolor(v, ++color);
bridges.push_back(edgeCorrection({u, v}));
}
else dfscolor(v, co);
}
}
void dfsdirect(int u)
{
visited[u] = 1;
for(int v : ad2[u]) if(visited[v] == 0){
//if(u == c[4]) cout<<"B"<<v<<endl;
dfsdirect(v); dp[u] += dp[v];
if(dp[v] != 0){
//if(v == c[4]) cout<<"B"<<u<<" "<<v<<endl;
pair<int, int> thisedge = edge_tree_to_graph[{u, v}];
if(dp[v] > 0){
if(c[thisedge.first] == u) ans[f[thisedge]] = 1;
else ans[f[thisedge]] = 2;
}
else{
if(c[thisedge.first] == u) ans[f[thisedge]] = 2;
else ans[f[thisedge]] = 1;
}
}
}
//cout<<u<<" "<<dp[u]<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin>>n>>m;
for(int i = 1; i <= m; i++){
int u, v;
cin>>u>>v;
if(u == v) continue;
ad[u].push_back(v);
ad[v].push_back(u);
edges.insert({u, v});
f[{u, v}] = i;
}
for(int i = 1; i <= n; i++) if(num[i] == 0){
dfs(i, -1);
color++;
dfscolor(i, color);
}
for(pair<int, int> i : bridges){
ad2[c[i.first]].push_back(c[i.second]);
ad2[c[i.second]].push_back(c[i.first]);
edge_tree_to_graph[{c[i.first], c[i.second]}] = i;
edge_tree_to_graph[{c[i.second], c[i.first]}] = i;
}
int p; cin>>p;
for(int i = 1; i <= p; i++){
int u, v;
cin>>u>>v;
dp[c[u]]++; dp[c[v]]--;
}
//cout<<"A"<<dp[c[4]]<<endl;
memset(visited, 0, sizeof(visited));
for(int i = 1; i <= color; i++) if(visited[i] == 0){
dfsdirect(i);
}
for(int i = 1; i <= m; i++){
if(ans[i] == 0) cout<<"B";
else if(ans[i] == 1) cout<<"L";
else cout<<"R";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7264 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7000 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7264 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7000 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
78 ms |
20936 KB |
Output is correct |
12 |
Correct |
79 ms |
21844 KB |
Output is correct |
13 |
Correct |
83 ms |
23176 KB |
Output is correct |
14 |
Correct |
105 ms |
26340 KB |
Output is correct |
15 |
Correct |
103 ms |
27596 KB |
Output is correct |
16 |
Correct |
213 ms |
37832 KB |
Output is correct |
17 |
Correct |
181 ms |
40132 KB |
Output is correct |
18 |
Correct |
185 ms |
38044 KB |
Output is correct |
19 |
Correct |
183 ms |
41672 KB |
Output is correct |
20 |
Correct |
84 ms |
21328 KB |
Output is correct |
21 |
Correct |
70 ms |
20968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7264 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7000 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
78 ms |
20936 KB |
Output is correct |
12 |
Correct |
79 ms |
21844 KB |
Output is correct |
13 |
Correct |
83 ms |
23176 KB |
Output is correct |
14 |
Correct |
105 ms |
26340 KB |
Output is correct |
15 |
Correct |
103 ms |
27596 KB |
Output is correct |
16 |
Correct |
213 ms |
37832 KB |
Output is correct |
17 |
Correct |
181 ms |
40132 KB |
Output is correct |
18 |
Correct |
185 ms |
38044 KB |
Output is correct |
19 |
Correct |
183 ms |
41672 KB |
Output is correct |
20 |
Correct |
84 ms |
21328 KB |
Output is correct |
21 |
Correct |
70 ms |
20968 KB |
Output is correct |
22 |
Correct |
224 ms |
40140 KB |
Output is correct |
23 |
Correct |
193 ms |
37832 KB |
Output is correct |
24 |
Correct |
202 ms |
37960 KB |
Output is correct |
25 |
Correct |
187 ms |
44228 KB |
Output is correct |
26 |
Correct |
200 ms |
39620 KB |
Output is correct |
27 |
Correct |
233 ms |
38068 KB |
Output is correct |
28 |
Correct |
40 ms |
9304 KB |
Output is correct |
29 |
Correct |
93 ms |
20896 KB |
Output is correct |
30 |
Correct |
80 ms |
20936 KB |
Output is correct |
31 |
Correct |
90 ms |
21384 KB |
Output is correct |
32 |
Correct |
127 ms |
26800 KB |
Output is correct |