This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1e5 + 69;
int n, m, p;
vector<int> graph[N];
int low[N], num[N], topo[N];
deque<int> st;
pair<int, int> edge[N];
void dfs(int u, int p){
num[u] = low[u] = ++num[0];
st.push_back(u);
for(int v: graph[u]) {
if (v == p){p = -1; continue;}
if (!num[v]){
dfs(v, u);
minimize(low[u], low[v]);
}
else minimize(low[u], num[v]);
}
if (num[u] == low[u]){
topo[0]++;
while(st.back() != u){
topo[st.back()] = topo[0];
st.pop_back();
}
st.pop_back();
topo[u] = topo[0];
}
}
vector<pair<int, int>> bridge_tree[N];
pair<int, int> par[N];
int h[N];
void go(int u, int p){
h[u] = h[p] + 1;
for(pair<int, int> v: bridge_tree[u]) if (v.first != p){
par[v.first] = {u, v.second};
go(v.first, u);
}
}
struct DSU{
int n;
vector<int> parent, sz, mi;
DSU(int _n){
n = _n;
parent.resize(n+1); sz.resize(n+1, 1);
for(int i = 1; i<=n; ++i) parent[i] = i;
mi = parent;
}
int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
bool same_set(int u, int v){return find_set(u) == find_set(v);}
bool join_set(int u, int v){
u = find_set(u), v = find_set(v);
if (u != v){
if (sz[u] < sz[v]) {swap(u, v); mi[u] = mi[v];}
parent[v] = u;
sz[u] += sz[v];
return true;
}
return false;
}
int get_size(int u){return sz[find_set(u)];}
int get_mi(int u){return mi[find_set(u)];}
};
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i<m; ++i){
int u, v; cin >> u >> v;
edge[i] = {u, v};
graph[u].push_back(v);
graph[v].push_back(u);
}
for(int i = 1; i<=n; ++i) if (num[i] == 0) {
dfs(i, 0);
}
string ans(m, 'B');
for(int i = 0; i<m; ++i){
int u = edge[i].first, v = edge[i].second;
u = topo[u], v = topo[v];
if (u == v) continue;
cout << u << " " << v << endl;
bridge_tree[u].push_back({v, i});
bridge_tree[v].push_back({u, i});
}
for(int i = 1; i<=topo[0]; ++i) if (h[i] == 0) {
go(i, 0);
}
cin >> p;
DSU mst(topo[0]);
for(int i = 0; i<p; ++i){
int u, v; cin >> u >> v;
u = topo[u], v = topo[v];
u = mst.get_mi(u); v = mst.get_mi(v);
while(u != v){
if (h[u] > h[v]){
ans[par[u].second] = 'R';
mst.join_set(par[u].first, u);
u = mst.get_mi(u);
}
else{
ans[par[v].second] = 'L';
mst.join_set(par[v].first, v);
v = mst.get_mi(v);
}
}
}
for(int i= 0; i<m; ++i) if (ans[i] != 'B'){
bool to_root = (ans[i] == 'R');
int u = edge[i].first, v = edge[i].second;
u = topo[u], v = topo[v];
if ((h[u] > h[v]) == to_root) ans[i] = 'R';
else ans[i] = 'L';
}
cout << ans << "\n";
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... |