#include "bits/stdc++.h"
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
vector<int> adj[MAXN];
int tin[MAXN], tout[MAXN], low[MAXN], vis[MAXN], tim = 0;
map<pair<int, int>, bool> is_bridge;
map<pair<int, int>, int> o; // appear count
void dfs(int node, int prv) {
tin[node] = ++tim;
int mi = 1e9;
vis[node] = 1;
for(int x: adj[node]) {
if(!vis[x]) {
dfs(x, node);
mi = min(mi, low[x]);
//cout << "tree edge: " << node << " -> " << x << "\n";
if(low[x] > tin[node] && o[{x, node}] == 1) {
//cout << "bridge found: " << x << " " << node << "\n";
is_bridge[{x, node}] = is_bridge[{node, x}] = 1;
}
}
}
tout[node] = ++tim;
//Calculating low[node]
low[node] = tin[node];
for(int x: adj[node]) {
if(tin[x] < tin[node] && tout[x] == 0 && tin[x] > 0 && x != prv) {
//cout << x << " is an ancestor of " << node << "\n";
low[node] = min(low[node], tin[x]);
}
}
low[node] = min(low[node], mi);
//cout << node << ": " << tin[node] << " " << tout[node] << " " << low[node] <<" \n";
}
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
vector<int> adj2[MAXN];
map<pair<int, int>, int> ord;
map<pair<int, int>, int> rel;
bool dfs2(int node, int prv, int tar) {
bool good = (node == tar);
if(!good) {
for(int x: adj2[node]) {
if(x != prv) {
if(dfs2(x, node, tar)) {
good = 1;
}
}
}
}
if(good) rel[{prv, node}] = rel[{node, prv}] = prv;
return good;
}
void solve(int tc) {
int n, m;
cin >> n >> m;
for(int i=1; i<=n; i++) {
dsu[i] = i;
}
pair<int, int> edges[m+1];
for(int i=1; i<=m; i++) {
int a, b;
cin >> a >> b;
edges[i] = {a, b};
o[{a, b}]++; o[{b, a}]++;
if(a == b) continue;
if(o[{a, b}] > 1) continue;
ord[edges[i]] = a;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1; i<=n; i++) {
if(!vis[i]) dfs(1, -1);
}
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(!is_bridge[{i, x}] || o[{i, x}] > 1) {
if(set_of(i) != set_of(x)) {
union_(i, x);
}
}
}
}
map<pair<int, int>, bool> alr;
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && !alr[{set_of(i), set_of(x)}]) {
alr[{set_of(i), set_of(x)}] = alr[{set_of(x), set_of(i)}] = 1;
adj2[set_of(i)].push_back(set_of(x));
adj2[set_of(x)].push_back(set_of(i));
}
}
}
//adj2 is the bridge-block tree of the graph
int p; cin >> p;
for(int i=0; i<p; i++) {
int x, y;
cin >> x >> y;
x = set_of(x), y = set_of(y);
if(x == y) continue;
dfs2(x, -1, y);
}
for(int i=1; i<=m; i++) {
if(edges[i].first == edges[i].second) {
cout << "B"; continue;
}
else if(o[edges[i]] > 1) {
cout << "B"; continue;
}
else if(!is_bridge[edges[i]]) {
cout << "B"; continue;
}
int r = rel[{set_of(edges[i].first), set_of(edges[i].second)}];
int q = set_of(ord[edges[i]]);
if(r > 0) cout << (r == q ? "R" : "L");
else cout << "B";
}
cout << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;// cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
5 6
1 2
1 2
4 3
2 3
1 3
5 1
1
1 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |