#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5 + 7;
const int infint = (int)1e9;
int visited[MAXN], is[MAXN], dp[MAXN], h[MAXN], n, m, par[MAXN], p[MAXN][20], up[MAXN], down[MAXN];
set< pair<int, int> > D;
pair<int, int> edge[MAXN];
vector<pair<int, int> > adj[MAXN];
vector<int> T[MAXN];
void dfs(int v, int index)
{
dp[v] = h[v];
visited[v] = 1;
for(auto u : adj[v])
{
if(!visited[u.first])
{
h[u.first] = h[v] + 1;
dfs(u.first, u.second);
dp[v] = min(dp[v], dp[u.first]);
}
else
{
if(index != u.second)
dp[v] = min(dp[v], h[u.first]);
}
}
if(index != -1)
{
if(dp[v] == h[v])
is[index] = 1;
}
return;
}
int get(int u)
{
return par[u] < 0 ? u : par[u] = get(par[u]);
}
void merge(int u, int v)
{
if((u = get(u)) == (v = get(v)))
return;
if(par[u] > par[v])
swap(u, v); //par[u] > par[v]
par[u] += par[v];
par[v] = u;
}
void add(int u, int v)
{
T[u].push_back(v);
T[v].push_back(u);
}
void dfs_pre(int u, int P)
{
visited[u] = 1, p[u][0] = P;
for (auto v : T[u])
if(v != P)
{
h[v] = h[u] + 1;
dfs_pre(v, u);
}
}
int lca(int u, int v)
{
if(h[u] > h[v])
swap(u, v);
for (int i = 19; i >= 0; i--)
if(h[v] - (1ll << i) >= h[u])
v = p[v][i];
if(u == v)
return u;
for (int i = 19; i >= 0; i--)
if(p[v][i] != p[u][i] && p[u][i] != -1 && p[v][i] != -1)
v = p[v][i], u = p[u][i];
return p[v][0];
}
void dfs_col(int u, int P)
{
for (auto v : T[u])
if(v != P)
{
dfs_col(v, u);
down[u] += down[v];
up[u] += up[v];
}
if(down[u])
D.insert({P, u});
if(up[u])
D.insert({u, P});
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> edge[i].first >> edge[i].second;
adj[edge[i].first].push_back({edge[i].second, i});
adj[edge[i].second].push_back({edge[i].first, i});
}
for (int i = 1; i <= n; i++)
if(!visited[i])
dfs(i, -1);
memset(par, -1, sizeof par);
for (int i = 0; i < m; i++)
if(!is[i])
merge(edge[i].first, edge[i].second);
for (int i = 0; i < m; i++)
if(get(edge[i].first) != get(edge[i].second))
{
int u = get(edge[i].first), v = get(edge[i].second);
add(u, v);
}
memset(visited, 0, sizeof visited);
memset(h, 0, sizeof h);
memset(p, -1, sizeof p);
for (int i = 1; i <= n; i++)
if(!visited[i])
{
add(0, i);
h[i] = 1;
dfs_pre(i, 0);
}
for (int i = 1; i < 20; i++)
for (int j = 1; j <= n; j++)
if(p[j][i - 1] != -1)
p[j][i] = p[p[j][i - 1]][i - 1];
//derakht moalefeyae yal hambandesho gereftim.
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
int x, y;
cin >> x >> y;
x = get(x), y = get(y);
int c = lca(x, y);
up[x]++, up[c]--;
down[y]++, down[c]--;
}
dfs_col(0, -1);
for (int i = 0; i < m; i++)
{
int u = get(edge[i].first), v = get(edge[i].second);
if(u == v)
cout << 'B';
else
if(D.count({u, v}))
cout << 'R';
else
if(D.count({v, u}))
cout << 'L';
else
cout << 'B';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14072 KB |
Output is correct |
2 |
Correct |
14 ms |
14072 KB |
Output is correct |
3 |
Correct |
18 ms |
14400 KB |
Output is correct |
4 |
Correct |
14 ms |
14448 KB |
Output is correct |
5 |
Correct |
14 ms |
14448 KB |
Output is correct |
6 |
Correct |
14 ms |
14448 KB |
Output is correct |
7 |
Correct |
14 ms |
14448 KB |
Output is correct |
8 |
Correct |
14 ms |
14448 KB |
Output is correct |
9 |
Correct |
13 ms |
14448 KB |
Output is correct |
10 |
Correct |
14 ms |
14448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14072 KB |
Output is correct |
2 |
Correct |
14 ms |
14072 KB |
Output is correct |
3 |
Correct |
18 ms |
14400 KB |
Output is correct |
4 |
Correct |
14 ms |
14448 KB |
Output is correct |
5 |
Correct |
14 ms |
14448 KB |
Output is correct |
6 |
Correct |
14 ms |
14448 KB |
Output is correct |
7 |
Correct |
14 ms |
14448 KB |
Output is correct |
8 |
Correct |
14 ms |
14448 KB |
Output is correct |
9 |
Correct |
13 ms |
14448 KB |
Output is correct |
10 |
Correct |
14 ms |
14448 KB |
Output is correct |
11 |
Correct |
69 ms |
19036 KB |
Output is correct |
12 |
Correct |
77 ms |
20460 KB |
Output is correct |
13 |
Correct |
91 ms |
22176 KB |
Output is correct |
14 |
Correct |
110 ms |
23948 KB |
Output is correct |
15 |
Correct |
151 ms |
24332 KB |
Output is correct |
16 |
Correct |
163 ms |
24332 KB |
Output is correct |
17 |
Correct |
223 ms |
27148 KB |
Output is correct |
18 |
Correct |
144 ms |
27148 KB |
Output is correct |
19 |
Correct |
194 ms |
28488 KB |
Output is correct |
20 |
Correct |
76 ms |
28488 KB |
Output is correct |
21 |
Correct |
82 ms |
28488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14072 KB |
Output is correct |
2 |
Correct |
14 ms |
14072 KB |
Output is correct |
3 |
Correct |
18 ms |
14400 KB |
Output is correct |
4 |
Correct |
14 ms |
14448 KB |
Output is correct |
5 |
Correct |
14 ms |
14448 KB |
Output is correct |
6 |
Correct |
14 ms |
14448 KB |
Output is correct |
7 |
Correct |
14 ms |
14448 KB |
Output is correct |
8 |
Correct |
14 ms |
14448 KB |
Output is correct |
9 |
Correct |
13 ms |
14448 KB |
Output is correct |
10 |
Correct |
14 ms |
14448 KB |
Output is correct |
11 |
Correct |
69 ms |
19036 KB |
Output is correct |
12 |
Correct |
77 ms |
20460 KB |
Output is correct |
13 |
Correct |
91 ms |
22176 KB |
Output is correct |
14 |
Correct |
110 ms |
23948 KB |
Output is correct |
15 |
Correct |
151 ms |
24332 KB |
Output is correct |
16 |
Correct |
163 ms |
24332 KB |
Output is correct |
17 |
Correct |
223 ms |
27148 KB |
Output is correct |
18 |
Correct |
144 ms |
27148 KB |
Output is correct |
19 |
Correct |
194 ms |
28488 KB |
Output is correct |
20 |
Correct |
76 ms |
28488 KB |
Output is correct |
21 |
Correct |
82 ms |
28488 KB |
Output is correct |
22 |
Correct |
335 ms |
29700 KB |
Output is correct |
23 |
Correct |
313 ms |
29700 KB |
Output is correct |
24 |
Correct |
289 ms |
29700 KB |
Output is correct |
25 |
Correct |
321 ms |
32812 KB |
Output is correct |
26 |
Correct |
337 ms |
32812 KB |
Output is correct |
27 |
Correct |
322 ms |
32812 KB |
Output is correct |
28 |
Correct |
58 ms |
32812 KB |
Output is correct |
29 |
Correct |
102 ms |
32812 KB |
Output is correct |
30 |
Correct |
113 ms |
32812 KB |
Output is correct |
31 |
Correct |
109 ms |
32812 KB |
Output is correct |
32 |
Correct |
184 ms |
32812 KB |
Output is correct |