#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "[" << p.first << ", " << p.second << "]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "["; for(int i = 0; i < (int)v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }
vector<vector<pair<int, int>>> adj;
vector<pair<int, int>> edges;
vector<bool> vis;
vector<int> dp, dep, start_below;
vector<int> start, en;
vector<pair<int, int>> back_edges; // alignment of the edges // 0 is don't flip
string ans;
void dfs(int u, int edgeID, int d = 0) {
// cout << pl(u) << endl;
if (vis[u]) return;
vis[u] = 1;
dep[u] = d;
start_below[u] = start[u] - en[u];
// cout << pl(u) << adj[u] << endl;
for (auto [v, id] : adj[u]) {
if (id == edgeID) continue;
if (!vis[v]) {
dfs(v, id, d + 1);
start_below[u] += start_below[v];
dp[u] += dp[v];
} else {
if (dep[u] > dep[v]) {
// goes up
dp[u]++;
} else {
dp[u]--;
}
}
}
// cout << pl(u) << pl(dp[u]) << endl;
if (edgeID != -1 && dp[u] == 0) {
// back edge
if (start_below[u] != 0) {
bool dir = start_below[u] > 0;
// check what direction the edge was originally oriented
if (edges[edgeID].first == u) {
dir = !dir;
}
back_edges.emplace_back(edgeID, dir);
}
}
}
void solve() {
int n, m; cin >> n >> m;
adj.resize(n);
// dfsTree.resize(n);
vis.resize(n);
start_below.resize(n);
dp.resize(n);
start.resize(n);
en.resize(n);
dep.resize(n);
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
a--, b--;
if (a == b) continue;
edges.emplace_back(a, b);
adj[a].emplace_back(b, i);
adj[b].emplace_back(a, i);
}
int p; cin >> p;
for (int i = 0; i < p; i++) {
int a, b; cin >> a >> b;
a--, b--;
start[a]++;
en[b]++;
}
for (int i = 0; i < n; i++) if (!vis[i]) dfs(i, -1, 0);
// cout << pl(vis[1]) << endl;
ans = string(m, 'B');
for (auto [edge, dir] : back_edges) {
ans[edge] = (!dir ? 'R' : 'L');
}
cout << ans << endl;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
34 ms |
7064 KB |
Output is correct |
12 |
Correct |
33 ms |
8316 KB |
Output is correct |
13 |
Correct |
41 ms |
10048 KB |
Output is correct |
14 |
Correct |
48 ms |
11632 KB |
Output is correct |
15 |
Correct |
47 ms |
11960 KB |
Output is correct |
16 |
Correct |
44 ms |
10420 KB |
Output is correct |
17 |
Correct |
58 ms |
12776 KB |
Output is correct |
18 |
Correct |
43 ms |
10576 KB |
Output is correct |
19 |
Correct |
44 ms |
13828 KB |
Output is correct |
20 |
Correct |
35 ms |
8556 KB |
Output is correct |
21 |
Correct |
39 ms |
8256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
34 ms |
7064 KB |
Output is correct |
12 |
Correct |
33 ms |
8316 KB |
Output is correct |
13 |
Correct |
41 ms |
10048 KB |
Output is correct |
14 |
Correct |
48 ms |
11632 KB |
Output is correct |
15 |
Correct |
47 ms |
11960 KB |
Output is correct |
16 |
Correct |
44 ms |
10420 KB |
Output is correct |
17 |
Correct |
58 ms |
12776 KB |
Output is correct |
18 |
Correct |
43 ms |
10576 KB |
Output is correct |
19 |
Correct |
44 ms |
13828 KB |
Output is correct |
20 |
Correct |
35 ms |
8556 KB |
Output is correct |
21 |
Correct |
39 ms |
8256 KB |
Output is correct |
22 |
Correct |
60 ms |
14440 KB |
Output is correct |
23 |
Correct |
64 ms |
12848 KB |
Output is correct |
24 |
Correct |
70 ms |
12964 KB |
Output is correct |
25 |
Correct |
60 ms |
17460 KB |
Output is correct |
26 |
Correct |
63 ms |
14124 KB |
Output is correct |
27 |
Correct |
60 ms |
12972 KB |
Output is correct |
28 |
Correct |
25 ms |
4476 KB |
Output is correct |
29 |
Correct |
48 ms |
9276 KB |
Output is correct |
30 |
Correct |
50 ms |
9448 KB |
Output is correct |
31 |
Correct |
53 ms |
9712 KB |
Output is correct |
32 |
Correct |
58 ms |
12436 KB |
Output is correct |