Submission #1181623

#TimeUsernameProblemLanguageResultExecution timeMemory
1181623nguyenkhangninh99One-Way Streets (CEOI17_oneway)C++20
100 / 100
55 ms23620 KiB

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;

int tin[maxn], low[maxn], scc[maxn], dp[maxn], edge[maxn][2], vis[maxn], timeDfs; 
char ans[maxn];

stack<int> st; 
vector<int> adj[maxn], g[maxn];

void dfs0(int u, int p){
    st.push(u);
    tin[u] = low[u] = ++timeDfs;

    for(int id: g[u]){
        if(id == p) continue;
        int v = edge[id][0] ^ edge[id][1] ^ u;
        if(!tin[v]) dfs0(v, id);
        low[u] = min(low[u], low[v]);
    }

    if(low[u] == tin[u]){
        while(true){
            int node = st.top();
            st.pop();
            scc[node] = u;
            if(node == u) return;
        }
    }
}

void dfs1(int x, int p){
    vis[x] = true;
    for(int id: adj[x]){
        int v = edge[id][0] ^ edge[id][1] ^ x;
        if(!vis[v]){
            dfs1(v, id);
            dp[x] += dp[v];
        }
    }

    if(p != -1){
        if(!dp[x]) ans[p] = 'B';
        else{
            if(dp[x] < 0) ans[p] = (x == edge[p][1] ? 'R' : 'L');
            else ans[p] = (x == edge[p][0] ? 'R' : 'L');
        }
    }
    return;
}

void solve(){
    int n, m; cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> edge[i][0] >> edge[i][1];
        g[edge[i][0]].push_back(i);
        g[edge[i][1]].push_back(i);
    }

    for(int i = 1; i <= n; i++){
        if(!tin[i]) dfs0(i, -1);
    }

    for(int i = 1; i <= m; i++){
        int u = edge[i][0], v = edge[i][1];
        if(scc[u] != scc[v]){
            adj[scc[u]].push_back(i);
            adj[scc[v]].push_back(i);
            edge[i][0] = scc[u];
            edge[i][1] = scc[v];
        }
        else ans[i] = 'B';
    }

    int q; cin >> q;
    for(int i = 1; i <= q; i++){
        int x, y; cin >> x >> y;
        dp[scc[x]]++;
        dp[scc[y]]--;
    }

    for(int i = 1; i <= n; i++){
        if(!vis[i]) dfs1(i, -1);
    }

    for(int i = 1; i <= m; i++) cout << ans[i];
    
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...