#include <bits/stdc++.h>
#define int long long
using namespace std;
const int NMAX = 100005;
int a, b;
vector<pair<int,int>> z[NMAX];
pair<int,int> edge[NMAX];
int id[NMAX], low[NMAX];
int cnt = 0;
int check[NMAX];
bool vis[NMAX];
int ver = 0;
int par[NMAX], sz[NMAX];
int mark[NMAX];
struct node {
int x, id, flip;
};
vector<node> z1[NMAX];
pair<int,int> rev[NMAX];
int ans[NMAX];
int high[NMAX];
int up[NMAX][25];
int sta[NMAX], fin[NMAX], tour = 0;
void dfs(int i, int parent) {
cnt++;
id[i] = low[i] = cnt;
for (auto p : z[i]) {
int nxt = p.first, eId = p.second;
if(nxt == parent) continue;
if(id[nxt])
low[i] = min(low[i], id[nxt]);
else {
dfs(nxt, i);
low[i] = min(low[i], low[nxt]);
if(low[nxt] >= id[i])
check[eId] = 1;
}
}
}
int findUF(int u) {
return (par[u] == u ? u : par[u] = findUF(par[u]));
}
void joinUF(int x, int y) {
x = findUF(x);
y = findUF(y);
if(x == y) return;
if(sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
par[y] = x;
}
void predfs(int i, int parent) {
tour++;
sta[i] = tour;
up[i][0] = parent;
for (auto p : z1[i]) {
if(p.x == parent) continue;
high[p.x] = high[i] + 1;
rev[p.x] = {p.id, p.flip};
predfs(p.x, i);
}
fin[i] = tour;
}
bool isanc(int x, int y) {
return sta[x] <= sta[y] && fin[x] >= fin[y];
}
int LCA(int u, int v) {
if(high[u] < high[v]) swap(u, v);
for (int i = 20; i >= 0; i--) {
if(high[u] - (1LL << i) >= high[v])
u = up[u][i];
}
if(u == v) return u;
for (int i = 20; i >= 0; i--) {
if(up[u][i] != up[v][i]) {
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
}
int diff[NMAX];
signed main(){
if (fopen("oneway.inp","r")){
freopen("oneway.inp","r",stdin);
freopen("oneway.out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a >> b;
for (int i = 1; i <= b; i++){
int x, y;
cin >> x >> y;
z[x].push_back({y, i});
z[y].push_back({x, i});
edge[i] = {x, y};
}
for (int i = 1; i <= a; i++){
if(!id[i])
dfs(i, 0);
}
for (int i = 1; i <= a; i++){
par[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= b; i++){
if(!check[i])
joinUF(edge[i].first, edge[i].second);
}
for (int i = 1; i <= a; i++){
int x = findUF(i);
if(!vis[x]){
vis[x] = true;
ver++;
mark[x] = ver;
}
mark[i] = mark[x];
}
for (int i = 1; i <= b; i++){
int x = mark[ edge[i].first ];
int y = mark[ edge[i].second ];
if(x == y) continue;
z1[x].push_back({y, i, 1});
z1[y].push_back({x, i, -1});
}
for (int i = 1; i <= ver; i++){
if(sta[i] == 0){
high[i] = 0;
predfs(i, 0);
}
}
for (int j = 1; j <= 20; j++){
for (int i = 1; i <= ver; i++){
int parNode = up[i][j-1];
if(parNode == 0)
up[i][j] = 0;
else
up[i][j] = up[ parNode ][j-1];
}
}
for (int i = 1; i <= ver; i++) diff[i] = 0;
int q;
cin >> q;
for (int i = 1; i <= q; i++){
int u, v;
cin >> u >> v;
int u_ = mark[u], v_ = mark[v];
if(u_ == v_) continue;
int L = LCA(u_, v_);
diff[u_] += 1;
diff[v_] -= 1;
diff[L] -= 1;
int parL = up[L][0];
if(parL != 0) diff[parL] -= 1;
}
vector<pair<int,int>> order;
for (int i = 1; i <= ver; i++){
order.push_back({high[i], i});
}
sort(order.begin(), order.end(), greater<pair<int,int>>());
for (auto &p : order){
int node = p.second;
int parNode = up[node][0];
if(parNode != 0)
diff[parNode] += diff[node];
}
for (int i = 2; i <= ver; i++){
int parNode = up[i][0];
if(parNode == 0) continue;
int eId = rev[i].first;
int val = diff[i];
if(val * rev[i].second > 0)
ans[eId] = 1;
else if(val * rev[i].second < 0)
ans[eId] = -1;
else
ans[eId] = 0;
}
for (int i = 1; i <= b; i++){
if(ans[i] == 0)
cout << "B";
else if(ans[i] < 0)
cout << "R";
else
cout << "L";
}
return 0;
}
/*
6
1 2
2 3
3 4
1 5
5 6
2
3 3
6 4
*/
/*
freopen("test.txt", "r", stdin);
freopen("o2.out", "w", stdout);
*/
/*
10 3 2 2
1 1 0
1 1 0
0 1 1
1 0 0
1 1 0
1 0 1
1 0 1
1 1 0
0 0 1
0 1 1
0
0
*/
컴파일 시 표준 에러 (stderr) 메시지
oneway.cpp: In function 'int main()':
oneway.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen("oneway.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen("oneway.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |