제출 #948631

#제출 시각아이디문제언어결과실행 시간메모리
948631GrindMachineOne-Way Streets (CEOI17_oneway)C++17
60 / 100
3042 ms31220 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; vector<array<int,3>> adj1[N]; vector<bool> vis(N); vector<bool> bridge(N); vector<int> tin(N), low(N); int timer = 1; void dfs1(int u, int pid){ vis[u] = 1; tin[u] = low[u] = timer++; for(auto [v,id,dir] : adj1[u]){ if(id == pid) conts; if(vis[v]){ amin(low[u],tin[v]); } else{ dfs1(v,id); amin(low[u],low[v]); if(low[v] > tin[u]){ bridge[id] = 1; } } } } vector<int> col(N); void dfs2(int u, int c){ vis[u] = 1; col[u] = c; for(auto [v,id,dir] : adj1[u]){ if(vis[v]) conts; if(bridge[id]) conts; dfs2(v,c); } } vector<pii> adj2[N]; struct lca_algo { // LCA template (for graphs with 1-based indexing) int LOG = 1; vector<int> depth; vector<vector<int>> up; vector<int> tin, tout; vector<int> pid; vector<bool> lca_vis; int timer = 1; lca_algo() { } lca_algo(int n) { lca_init(n); } void lca_init(int n) { while ((1 << LOG) < n) LOG++; up = vector<vector<int>>(n + 1, vector<int>(LOG, 1)); depth = vector<int>(n + 1); tin = vector<int>(n + 1); tout = vector<int>(n + 1); pid = vector<int>(n + 1); lca_vis = vector<bool>(n + 1); rep1(i,n){ if(!lca_vis[i]){ lca_dfs(i,-1); } } } void lca_dfs(int node, int par) { lca_vis[node] = 1; tin[node] = timer++; for(auto [child, id] : adj2[node]) { if (child == par) conts; up[child][0] = node; rep1(j, LOG - 1) { up[child][j] = up[up[child][j - 1]][j - 1]; } depth[child] = depth[node] + 1; pid[child] = id; lca_dfs(child, node); } tout[node] = timer-1; } int lift(int u, int k) { rep(j, LOG) { if (k & (1 << j)) { u = up[u][j]; } } return u; } int query(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int k = depth[u] - depth[v]; u = lift(u, k); if (u == v) return u; rev(j, LOG - 1, 0) { if (up[u][j] != up[v][j]) { u = up[u][j]; v = up[v][j]; } } u = up[u][0]; return u; } int get_dis(int u, int v) { int lca = query(u, v); return depth[u] + depth[v] - 2 * depth[lca]; } bool is_ances(int u, int v){ return tin[u] <= tin[v] and tout[u] >= tout[v]; } }; void solve(int test_case) { int n,m; cin >> n >> m; vector<pii> edges(m+5); rep1(i,m){ int u,v; cin >> u >> v; edges[i] = {u,v}; adj1[u].pb({v,i,0}), adj1[v].pb({u,i,1}); } int k; cin >> k; vector<pii> a(k+5); rep1(i,k) cin >> a[i].ff >> a[i].ss; rep1(u,n){ if(vis[u]) conts; dfs1(u,-1); } rep1(u,n){ assert(vis[u]); } fill(all(vis),0); int ptr = 0; rep1(u,n){ if(vis[u]) conts; dfs2(u,++ptr); } rep1(u,n){ for(auto [v,id,dir] : adj1[u]){ if(dir) conts; if(col[u] != col[v]){ int cu = col[u], cv = col[v]; adj2[cu].pb({cv,id}), adj2[cv].pb({cu,id}); edges[id] = {cu,cv}; } } } lca_algo LCA(ptr); vector<char> ans(m+5); rep1(i,m) ans[i] = 'B'; rep1(i,k){ auto [u,v] = a[i]; u = col[u], v = col[v]; int lca = LCA.query(u,v); while(u != lca){ int p = LCA.up[u][0]; int id = LCA.pid[u]; if(u == edges[id].ff){ ans[id] = 'R'; } else{ ans[id] = 'L'; } u = p; } while(v != lca){ int p = LCA.up[v][0]; int id = LCA.pid[v]; if(v == edges[id].ff){ ans[id] = 'L'; } else{ ans[id] = 'R'; } v = p; } } rep1(i,m) cout << ans[i]; cout << endl; } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...