#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXL = 20;
int n, m, q, tim;
int tin[MAXN], low[MAXN], com[MAXN], u[MAXN], v[MAXN], dp[MAXN];
bool Vis[MAXN];
char ans[MAXN];
vector <pair <int, int> > Adj[MAXN];
bool Bridge[MAXN];
void Build(int node, int p = -1)
{
tim++;
tin[node] = tim;
low[node] = tim;
Vis[node] = true;
for(auto x : Adj[node])
{
if(x.second == p)
{
continue;
}
if(Vis[x.first])
{
low[node] = min(low[node], tin[x.first]);
}
else
{
Build(x.first, x.second);
if(low[x.first] > tin[node])
{
Bridge[x.second] = true;
}
low[node] = min(low[node], low[x.first]);
}
}
}
void BuildNew(int node, int c)
{
com[node] = c;
for(auto x : Adj[node])
{
if(Bridge[x.second])
{
continue;
}
ans[x.second] = 'B';
if(com[x.first])
{
continue;
}
BuildNew(x.first, c);
}
}
void DFS(int node, int p = -1)
{
Vis[node] = true;
for(auto x : Adj[node])
{
if(x.first == p)
{
continue;
}
DFS(x.first, node);
}
}
void DFS2(int node, int p = -1)
{
Vis[node] = true;
for(auto x : Adj[node])
{
if(x.first == p)
{
continue;
}
DFS2(x.first, node);
if(dp[x.first] < 0)
{
ans[x.second] = (u[x.second] == node ? 'R' : 'L');
}
else if(dp[x.first] > 0)
{
ans[x.second] = (v[x.second] == node ? 'R' : 'L');
}
else
{
ans[x.second] = 'B';
}
dp[node] += dp[x.first];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> u[i] >> v[i];
Adj[u[i]].emplace_back(v[i], i);
Adj[v[i]].emplace_back(u[i], i);
}
for(int i = 1; i <= n; i++)
{
if(!Vis[i])
{
Build(i);
}
}
tim = 0;
for(int i = 1; i <= n; i++)
{
if(!com[i])
{
tim++;
BuildNew(i, tim);
}
}
for(int i = 1; i <= n; i++)
{
Adj[i].clear();
Vis[i] = false;
}
for(int i = 1; i <= m; i++)
{
u[i] = com[u[i]], v[i] = com[v[i]];
if(u[i] != v[i])
{
Adj[u[i]].emplace_back(v[i], i);
Adj[v[i]].emplace_back(u[i], i);
}
}
for(int i = 1; i <= tim; i++)
{
if(!Vis[i])
{
DFS(i);
}
}
cin >> q;
while(q--)
{
int x, y;
cin >> x >> y;
x = com[x];
y = com[y];
dp[x]++;
dp[y]--;
}
for(int i = 1; i <= tim; i++)
{
Vis[i] = false;
}
for(int i = 1; i <= tim; i++)
{
if(!Vis[i])
{
DFS2(i);
}
}
for(int i = 1; i <= m; i++)
{
cout << ans[i];
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
2 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2764 KB |
Output is correct |
7 |
Correct |
2 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
2 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2764 KB |
Output is correct |
7 |
Correct |
2 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2764 KB |
Output is correct |
11 |
Correct |
33 ms |
8812 KB |
Output is correct |
12 |
Correct |
38 ms |
9796 KB |
Output is correct |
13 |
Correct |
44 ms |
11016 KB |
Output is correct |
14 |
Correct |
59 ms |
12048 KB |
Output is correct |
15 |
Correct |
52 ms |
12092 KB |
Output is correct |
16 |
Correct |
55 ms |
10208 KB |
Output is correct |
17 |
Correct |
53 ms |
11708 KB |
Output is correct |
18 |
Correct |
60 ms |
10208 KB |
Output is correct |
19 |
Correct |
48 ms |
12868 KB |
Output is correct |
20 |
Correct |
44 ms |
9520 KB |
Output is correct |
21 |
Correct |
36 ms |
9268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
2 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2764 KB |
Output is correct |
7 |
Correct |
2 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2764 KB |
Output is correct |
11 |
Correct |
33 ms |
8812 KB |
Output is correct |
12 |
Correct |
38 ms |
9796 KB |
Output is correct |
13 |
Correct |
44 ms |
11016 KB |
Output is correct |
14 |
Correct |
59 ms |
12048 KB |
Output is correct |
15 |
Correct |
52 ms |
12092 KB |
Output is correct |
16 |
Correct |
55 ms |
10208 KB |
Output is correct |
17 |
Correct |
53 ms |
11708 KB |
Output is correct |
18 |
Correct |
60 ms |
10208 KB |
Output is correct |
19 |
Correct |
48 ms |
12868 KB |
Output is correct |
20 |
Correct |
44 ms |
9520 KB |
Output is correct |
21 |
Correct |
36 ms |
9268 KB |
Output is correct |
22 |
Correct |
69 ms |
12860 KB |
Output is correct |
23 |
Correct |
74 ms |
11372 KB |
Output is correct |
24 |
Correct |
75 ms |
11328 KB |
Output is correct |
25 |
Correct |
66 ms |
15864 KB |
Output is correct |
26 |
Correct |
82 ms |
12836 KB |
Output is correct |
27 |
Correct |
70 ms |
11460 KB |
Output is correct |
28 |
Correct |
27 ms |
6852 KB |
Output is correct |
29 |
Correct |
52 ms |
10140 KB |
Output is correct |
30 |
Correct |
50 ms |
10352 KB |
Output is correct |
31 |
Correct |
53 ms |
10564 KB |
Output is correct |
32 |
Correct |
56 ms |
12636 KB |
Output is correct |