이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 1e5;
const int LogN = 31 - __builtin_clz(N);
int n, m, nQuery, lab[N + 2], h[N + 2], par[N + 2][LogN + 2], lgn, num[N + 2], low[N + 2],
group[N + 2], timeDFS = 0, nGroup = 0, dir[N + 2][2];
bool bridge[N + 2];
vector<pair<int, int> > fadj[N + 2];
vector<int> adj[N + 2];
pair<int, int> edge[N + 2], que[N + 2];
void input() {
cin >> n >> m;
int u, v;
for(int i = 1; i <= m; i++) {
cin >> u >> v;
edge[i] = make_pair(u, v);
fadj[u].push_back(make_pair(v, i));
fadj[v].push_back(make_pair(u, i));
}
cin >> nQuery;
for(int i = 1; i <= nQuery; i++) {
cin >> que[i].fi >> que[i].se;
}
}
int getRoot(int u) {
return lab[u] < 0 ? u : lab[u] = getRoot(lab[u]);
}
bool unite(int u, int v) {
u = getRoot(u);
v = getRoot(v);
if(u == v) return false;
if(lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
void DFS(int u) {
for(int v : adj[u]) {
if(v == par[u][0]) continue;
h[v] = h[u] + 1;
par[v][0] = u;
for(int j = 1; j <= lgn; j++) par[v][j] = par[par[v][j - 1]][j - 1];
DFS(v);
}
}
int getLCA(int u, int v) {
if(h[u] != h[v]) {
if(h[u] < h[v]) swap(u, v);
int dif = h[u] - h[v];
for(int i = 0; (1 << i) <= dif; i++) {
if((dif >> i) & 1) u = par[u][i];
}
}
if(u == v) return u;
for(int j = lgn; j >= 0; j--) {
if(par[u][j] == par[v][j]) continue;
u = par[u][j];
v = par[v][j];
}
return par[u][0];
}
void tarjan(int u, int p) {
num[u] = low[u] = ++timeDFS;
for(pair<int, int> e : fadj[u]) {
int v = e.fi;
int id = e.se;
if(id == p) continue;
if(num[v]) low[u] = min(low[u], num[v]);
else {
tarjan(v, id);
low[u] = min(low[u], low[v]);
if(low[v] == num[v]) bridge[id] = 1;
}
}
}
void calc(int u) {
for(int v : adj[u]) {
if(v == par[u][0]) continue;
calc(v);
dir[u][0] += dir[v][0];
dir[u][1] += dir[v][1];
}
}
void solve() {
lgn = 31 - __builtin_clz(n);
for(int i = 1; i <= n; i++) {
if(!num[i]) tarjan(i, 0);
}
memset(lab, -1, sizeof lab);
for(int i = 1; i <= m; i++) {
if(bridge[i]) {
continue;
}
int u = edge[i].fi;
int v = edge[i].se;
unite(u, v);
}
for(int i = 1; i <= n; i++) {
if(lab[i] < 0) group[i] = ++nGroup;
}
for(int i = 1; i <= n; i++) {
group[i] = group[getRoot(i)];
}
for(int i = 1; i <= m; i++) {
if(bridge[i]) {
adj[group[edge[i].fi]].push_back(group[edge[i].se]);
adj[group[edge[i].se]].push_back(group[edge[i].fi]);
}
}
for(int i = 1; i <= nGroup; i++) {
if(par[i][0] == 0) DFS(i);
}
for(int i = 1; i <= nQuery; i++) {
int u = group[que[i].fi];
int v = group[que[i].se];
int lca = getLCA(u, v);
dir[u][0]++;
dir[v][1]++;
dir[lca][0]--;
dir[lca][1]--;
}
for(int i = 1; i <= nGroup; i++) {
if(par[i][0]) calc(i);
}
for(int i = 1; i <= m; i++) {
if(!bridge[i]) {
cout << 'B';
continue;
}
int u = group[edge[i].fi];
int v = group[edge[i].se];
if(h[u] < h[v]) {
if(dir[v][0] > 0) cout << 'L';
else if(dir[v][1] > 0) cout << 'R';
else cout << 'B';
} else {
if(dir[u][0] > 0) cout << 'R';
else if(dir[u][1] > 0) cout << 'L';
else cout << 'B';
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(" .inp", "r", stdin);
// freopen(" .out", "w", stdout);
input();
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |