#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
const ll oo = 1e18 + 5;
const int MAXN = 1e5 + 5;
const int LOG = 16;
//#define int ll
int n, m, TimerDFS, low[MAXN], num[MAXN], scc_quanity, scc_id[MAXN], par[MAXN][LOG + 1], ans[MAXN];
int high[MAXN], f[MAXN];
pair<int, int> edges[MAXN], e[MAXN];
vector<pair<int, int>> adj[MAXN];
vector<pair<int, int>> g[MAXN];
bool del[MAXN];
bool vis[MAXN];
stack<int> st;
void DFS_tarjan(int u, int pid) {
++TimerDFS;
st.push(u);
// cout << u << '\n';
num[u] = low[u] = TimerDFS;
for (int i = 0; i < int(adj[u].size()); ++i) {
int v = adj[u][i].first, nid = adj[u][i].second;
if (pid != nid && del[v] == false){
if (num[v] == 0) {
DFS_tarjan(v, nid);
low[u] = min(low[u], low[v]);
} else low[u] = min(low[u], num[v]);
}
}
if (low[u] == num[u]) {
int v;
++scc_quanity;
do {
v = st.top();
del[v] = true;
st.pop();
scc_id[v] = scc_quanity;
} while (v != u);
}
}
//void DFS_high(int u, int p) {
// for (int i = 0; i < int(g[u].size()); ++i) {
// int v = g[u][i].f;
// if (v != p) {
// high[v] = high[u] + 1;
// par[v][0] = u;
// DFS_high(v, u);
// }
// }
//}
//void prepare_LCA() {
// high[0] = -1;
// DFS_high(1, -1);
// for (int j = 1; j <= LOG; ++j) {
// for (int i = 1; i <= n; ++i) {
// par[i][j] = par[par[i][j - 1]][j - 1];
// }
// }
////}
//int LCA(int u, int v) {
// if (high[u] < high[v]) return LCA(v, u);
//
// int k = high[u] - high[v];
//
// for (int i = 0; i <= LOG; ++i) {
// if ((k >> i) & 1) {
// u = par[u][i];
// }
// }
//
// if (u == v) return u;
//
// for (int i = LOG; i >= 0; --i) {
// if (par[u][i] != par[v][i]) {
// u = par[u][i];
// v = par[v][i];
// }
// }
// return par[u][0];
//}
void DFS_solve(int u, int p) {
vis[u] = true;
for (int i = 0; i < int(g[u].size()); ++i) {
int v = g[u][i].first, id = g[u][i].second;
if (v != p) {
if (!vis[v])
DFS_solve(v, u);
f[u] += f[v];
if (f[v] == 0) ans[id] = 0;
else if (f[v] < 0) ans[id] = (v == e[id].second ? 2 : 1);
else ans[id] = (v == e[id].second ? 1 : 2);
}
}
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int u, v; cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
edges[i] = {u, v};
}
for (int i = 1; i <= n; ++i)
if (num[i] == 0) DFS_tarjan(i, -1);
// cout << TimerDFS << '\n';
// cout << scc_quanity << '\n';
// for (int i = 1; i <= n; ++i) {
// cout << scc_id[i] << ' ';
// }
// cout << '\n';
for (int i = 1; i <= m; ++i) {
int u = scc_id[edges[i].first], v = scc_id[edges[i].second];
// vi la vo huong nen ko co canh trung
if (u != v) {
g[u].push_back({v, i});
g[v].push_back({u, i});
e[i] = {u, v};
}
}
// for (int i = 1; i <= scc)
// prepare_LCA();
// DFS_high(1, -1);
int q; cin >> q;
while (q--) {
int u, v; cin >> u >> v;
u = scc_id[u];
v = scc_id[v];
// int uv = LCA(u, v);
++f[u];
--f[v];
// cout << u << ' ' << v << '\n';
// f[uv] -= 3;
}
for (int i = 1; i <= scc_quanity; ++i)
if (!vis[i])
DFS_solve(i, -1);
for (int i = 1; i <= m; ++i) {
if (ans[i] == 0) cout << "B";
else if (ans[i] == 1) cout << "L";
else cout << "R";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
// freopen("phanluong.inp", "r", stdin);
// freopen("phanluong.out", "w", stdout);
// int T = 1; cin >> T; while(T--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10860 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
8680 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
6664 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10860 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
8680 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
6664 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
30 ms |
12120 KB |
Output is correct |
12 |
Correct |
31 ms |
16988 KB |
Output is correct |
13 |
Correct |
40 ms |
18004 KB |
Output is correct |
14 |
Correct |
36 ms |
17232 KB |
Output is correct |
15 |
Correct |
34 ms |
16076 KB |
Output is correct |
16 |
Correct |
39 ms |
17492 KB |
Output is correct |
17 |
Correct |
40 ms |
18860 KB |
Output is correct |
18 |
Correct |
51 ms |
17416 KB |
Output is correct |
19 |
Correct |
62 ms |
20948 KB |
Output is correct |
20 |
Correct |
27 ms |
16476 KB |
Output is correct |
21 |
Correct |
25 ms |
16208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10860 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
8680 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
6664 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
30 ms |
12120 KB |
Output is correct |
12 |
Correct |
31 ms |
16988 KB |
Output is correct |
13 |
Correct |
40 ms |
18004 KB |
Output is correct |
14 |
Correct |
36 ms |
17232 KB |
Output is correct |
15 |
Correct |
34 ms |
16076 KB |
Output is correct |
16 |
Correct |
39 ms |
17492 KB |
Output is correct |
17 |
Correct |
40 ms |
18860 KB |
Output is correct |
18 |
Correct |
51 ms |
17416 KB |
Output is correct |
19 |
Correct |
62 ms |
20948 KB |
Output is correct |
20 |
Correct |
27 ms |
16476 KB |
Output is correct |
21 |
Correct |
25 ms |
16208 KB |
Output is correct |
22 |
Correct |
51 ms |
20048 KB |
Output is correct |
23 |
Correct |
58 ms |
18416 KB |
Output is correct |
24 |
Correct |
64 ms |
21600 KB |
Output is correct |
25 |
Correct |
58 ms |
27496 KB |
Output is correct |
26 |
Correct |
61 ms |
21276 KB |
Output is correct |
27 |
Correct |
65 ms |
21352 KB |
Output is correct |
28 |
Correct |
22 ms |
11968 KB |
Output is correct |
29 |
Correct |
57 ms |
15440 KB |
Output is correct |
30 |
Correct |
41 ms |
17236 KB |
Output is correct |
31 |
Correct |
36 ms |
15964 KB |
Output is correct |
32 |
Correct |
45 ms |
20308 KB |
Output is correct |