#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector <vector<ar<int,2>>> adj(n);
vector <ar<int,2>> E;
map <int,map<int,int>> mp;
for ( int i = 0; i < m; i++ ){
int u, v; cin >> u >> v;
--u, --v;
adj[u].pb({v, i});
adj[v].pb({u, i});
E.pb({u, v});
mp[u][v]++;
mp[v][u]++;
}
int p; cin >> p;
vector <vector<ar<int,2>>> q(n);
for ( int i = 0; i < p; i++ ){
int x, y; cin >> x >> y;
--x, --y;
q[x].pb({y, 0});
q[y].pb({x, 1});
}
vector <int> us(n), tin(n), tout(n), dir(m);
int ct = 0;
auto trav = [&](auto trav, int u, int p) -> void{
us[u] = true;
tin[u] = ++ct;
for ( auto &[v, j]: adj[u] ){
if ( v != p && !us[v] ){
if ( E[j] == ar<int,2>{u, v} ){
dir[j] = 1;
}
trav(trav, v, u);
}
} tout[u] = ct;
};
vector <int> roots;
for ( int i = 0; i < n; i++ ){
if ( !us[i] ){
roots.pb(i);
trav(trav, i, -1);
}
}
vector <int> low(n), ans(m, -1);
vector <pair<int,int>> mn(n), mx(n);
auto dfs = [&](auto dfs, int u, int p) -> void{
mn[u] = {n + 1, -1};
mx[u] = {0, -1};
for ( auto &[v, t]: q[u] ){
chmin(mn[u], pair<int,int>{tin[v], t});
chmax(mx[u], pair<int,int>{tout[v], t});
}
low[u] = tin[u];
vector <ar<int,2>> tmp;
for ( auto &[v, j]: adj[u] ){
if ( v != p ){
if ( low[v] ){
chmin(low[u], tin[v]);
} else{
dfs(dfs, v, u);
chmin(mn[u], mn[v]);
chmax(mx[u], mx[v]);
chmin(low[u], low[v]);
if ( low[v] > tin[u] ){
tmp.pb({v, j});
}
}
}
}
for ( auto &[v, j]: tmp ){
if ( mn[v].first < tin[v] ){
ans[j] = mn[v].second;
}
if ( mx[v].first > tout[v] ){
ans[j] = mx[v].second;
}
}
};
for ( auto &u: roots ){
dfs(dfs, u, -1);
}
for ( int i = 0; i < m; i++ ){
auto [u, v] = E[i];
if ( mp[u][v] > 1 ){
ans[i] = -1;
}
}
int it = 0;
for ( auto &x: ans ){
x ^= dir[it++];
cout << (x > 0 ? 'L' : x < 0 ? 'B' : 'R');
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
149 ms |
29364 KB |
Output is correct |
12 |
Correct |
170 ms |
33760 KB |
Output is correct |
13 |
Correct |
192 ms |
39376 KB |
Output is correct |
14 |
Correct |
226 ms |
45508 KB |
Output is correct |
15 |
Correct |
232 ms |
46784 KB |
Output is correct |
16 |
Correct |
228 ms |
42688 KB |
Output is correct |
17 |
Correct |
195 ms |
48068 KB |
Output is correct |
18 |
Correct |
229 ms |
42688 KB |
Output is correct |
19 |
Correct |
178 ms |
51504 KB |
Output is correct |
20 |
Correct |
171 ms |
36060 KB |
Output is correct |
21 |
Correct |
131 ms |
34568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
149 ms |
29364 KB |
Output is correct |
12 |
Correct |
170 ms |
33760 KB |
Output is correct |
13 |
Correct |
192 ms |
39376 KB |
Output is correct |
14 |
Correct |
226 ms |
45508 KB |
Output is correct |
15 |
Correct |
232 ms |
46784 KB |
Output is correct |
16 |
Correct |
228 ms |
42688 KB |
Output is correct |
17 |
Correct |
195 ms |
48068 KB |
Output is correct |
18 |
Correct |
229 ms |
42688 KB |
Output is correct |
19 |
Correct |
178 ms |
51504 KB |
Output is correct |
20 |
Correct |
171 ms |
36060 KB |
Output is correct |
21 |
Correct |
131 ms |
34568 KB |
Output is correct |
22 |
Correct |
228 ms |
54252 KB |
Output is correct |
23 |
Correct |
235 ms |
49848 KB |
Output is correct |
24 |
Correct |
265 ms |
49088 KB |
Output is correct |
25 |
Correct |
215 ms |
63400 KB |
Output is correct |
26 |
Correct |
230 ms |
53032 KB |
Output is correct |
27 |
Correct |
221 ms |
49604 KB |
Output is correct |
28 |
Correct |
59 ms |
13048 KB |
Output is correct |
29 |
Correct |
189 ms |
41100 KB |
Output is correct |
30 |
Correct |
173 ms |
41576 KB |
Output is correct |
31 |
Correct |
191 ms |
42436 KB |
Output is correct |
32 |
Correct |
168 ms |
47040 KB |
Output is correct |