Submission #647472

#TimeUsernameProblemLanguageResultExecution timeMemory
647472fahimcp495One-Way Streets (CEOI17_oneway)C++17
100 / 100
141 ms31428 KiB
#include<bits/stdc++.h> using namespace std; #define dbg(a) cerr << #a << ": " << a << "\n" using ll = long long; struct graph { int n, t, sz; vector<vector<int>> adj; vector<int> tin, low, cmp; graph(int n): n(n),adj(n),tin(n),low(n),cmp(n){} void add_edge(int u, int v){ adj[u].push_back(v); adj[v].push_back(u); } void dfs(int u, int p){ tin[u]=low[u]=t++; int cnt=0; for(int v: adj[u]){ if(v==p and ++cnt <= 1) continue; if(tin[v]!=-1) low[u] = min(low[u], tin[v]); else { dfs(v,u); low[u] = min(low[u], low[v]); } } } void dfs2(int u, int p){ if(p!=-1 and tin[p]>=low[u]) cmp[u] = cmp[p]; else cmp[u] = sz++; for(int v: adj[u]){ if(cmp[v]==-1) dfs2(v,u); } } void process_2ecc(){ t = 0, sz = 0; for (int i = 0; i < n; ++i){ tin[i] = low[i] = cmp[i] = -1; } for (int i = 0; i < n; ++i){ if(tin[i]==-1) dfs(i,-1); } for (int i = 0; i < n; ++i){ if(cmp[i]==-1) dfs2(i,-1); } } }; const int N = 1e5+5, LG = 20; int par[N][LG]; vector<array<int, 3>> adj[N]; vector<int> dep(N), tin(N, -1), tout(N), mn_left(N, N), mn_right(N, N); char ans[N]; int t = 0; void dfs(int u, int p) { tin[u] = t++; for (int k = 1; k < LG; ++k) { par[u][k] = par[par[u][k - 1]][k - 1]; } for (auto [v, e, dir]: adj[u]) { if (v != p) { dep[v] = dep[u] + 1; par[v][0] = u; dfs(v, u); } } tout[u] = t++; } bool is_anc(int u, int v) { return (tin[u] <= tin[v] and tout[v] <= tout[u]); } int get_lca(int u, int v) { if (is_anc(u, v)) return u; if (is_anc(v, u)) return v; for (int j = LG - 1; j >= 0; --j) { if (!is_anc(par[u][j], v)) { u = par[u][j]; } } return par[u][0]; } vector<int> vis(N); void dfs2(int u, int p) { vis[u] = 1; for (auto [v, e, dir]: adj[u]) { if (!vis[v]) { dfs2(v, u); if (mn_left[v] <= dep[u]) { // v -> u if (dir == 0) { // ans[e] = 'L'; } else { ans[e] = 'R'; } } if (mn_right[v] <= dep[u]) { // u -> v if (dir == 0) { ans[e] = 'R'; } else { ans[e] = 'L'; } } mn_left[u] = min(mn_left[u], mn_left[v]); mn_right[u] = min(mn_right[u], mn_right[v]); } } } void solve() { int n, m; cin >> n >> m; graph g(n); vector<array<int, 2>> edges(m); for (auto &[u, v]: edges) { cin >> u >> v; u--, v--; if (u != v) { g.add_edge(u, v); } } g.process_2ecc(); for (int e = 0; e < m; ++e) { ans[e] = 'B'; auto [u, v] = edges[e]; u = g.cmp[u]; v = g.cmp[v]; if (u != v) { adj[u].push_back({v, e, 0}); adj[v].push_back({u, e, 1}); } } for (int u = 0; u < n; ++u) { if (tin[u] == -1) { par[u][0] = u; dfs(u, -1); } } int q; cin >> q; while (q--) { int u, v; cin >> u >> v; u--, v--; u = g.cmp[u]; v = g.cmp[v]; int l = get_lca(u, v); mn_left[u] = min(mn_left[u], dep[l]); mn_right[v] = min(mn_right[v], dep[l]); } for (int u = 0; u < n; ++u) { if (!vis[u]) { dfs2(u, u); } } cout << ans << "\n"; } int main(){ ios::sync_with_stdio(0), cin.tie(0); int tc = 1; for (int t = 1; t <= tc; ++t) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...