제출 #939558

#제출 시각아이디문제언어결과실행 시간메모리
939558phoenix0423One-Way Streets (CEOI17_oneway)C++17
100 / 100
83 ms37404 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
const int maxn = 2e5 + 5;
int n, m, q;
vector<pll> adj[maxn];
vector<pll> nadj[maxn];
int scc[maxn], low[maxn], in[maxn], dfn = 0, cnt = 0;
int val[maxn];
pll ori[maxn];
stack<int> st;

void dfs(int pos, int pid){
    low[pos] = in[pos] = ++dfn;
    st.push(pos);
    for(auto [x, id] : adj[pos]){
        if(id == pid) continue;
        if(in[x]) low[pos] = min(low[pos], in[x]);
        else{
            dfs(x, id);
            low[pos] = min(low[pos], low[x]);
        }
    }
    if(low[pos] >= in[pos]){
        cnt++;
        while(st.top() != pos){
            scc[st.top()] = cnt;
            st.pop();
        }
        scc[pos] = cnt;
        st.pop();
    }
}

void dfs2(int pos, int prev){
    in[pos] = -1;
    for(auto [x, id] : nadj[pos]){
        if(x == prev) continue;
        dfs2(x, pos);
        if(val[x] > 0) ori[id] = {x, pos};
        if(val[x] < 0) ori[id] = {pos, x};
        val[pos] += val[x];
    }
}
signed main(void){
    fastio;
    cin>>n>>m;
    vector<pll> e;
    for(int i = 0; i < m; i++){
        int a, b;
        cin>>a>>b;
        a--, b--;
        adj[a].eb(b, i);
        adj[b].eb(a, i);
        e.pb({a, b});
    }
    for(int i = 0; i < n; i++) if(!in[i]) dfs(i, -1);
    for(int i = 0; i < n; i++){
        for(auto [x, id] : adj[i]){
            if(scc[i] != scc[x]){
                nadj[scc[i]].eb(scc[x], id);
            }
        }
    }
    cin>>q;
    for(int i = 0; i < q; i++){
        int a, b;
        cin>>a>>b;
        a--, b--;
        val[scc[a]] ++, val[scc[b]] --;
    }
    for(int i = 1; i <= cnt; i++) if(in[i] != -1) dfs2(i, -1);
    for(int i = 0; i < m; i++){
        if(ori[i].f == ori[i].s) cout<<'B';
        else if(ori[i] == pll(scc[e[i].f], scc[e[i].s])) cout<<'R';
        else cout<<'L';
    }
    cout<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...