Submission #1132863

#TimeUsernameProblemLanguageResultExecution timeMemory
1132863InvMODOne-Way Streets (CEOI17_oneway)C++20
100 / 100
73 ms23880 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;


int n, m, q, timerDFS, cntSCC;

int tin[N], low[N], scc[N], dp[N]; char answer[N];

stack<int> st; pair<int,int> E[N];

vector<int> adj[N], g[N]; bool vis[N];

void Tarjan(int x, int p){
    st.push(x);
    tin[x] = low[x] = ++timerDFS;

    for(int id : g[x]){
        if(id != p){
            int v = E[id].fi ^ E[id].se ^ x;

            if(!tin[v]) Tarjan(v, id);
            ckmn(low[x], low[v]);
        }
    }

    if(low[x] >= tin[x]){
        int tmp; ++cntSCC;
        do{
            tmp = st.top(); st.pop();

            tin[tmp] = low[tmp] = 2 * n + 1;
            scc[tmp] = cntSCC;
        }while(tmp != x);
    }
}

void dfs(int x, int id){
    vis[x] = true;
    for(int id : adj[x]){
        int v = E[id].fi ^ E[id].se ^ x;
        if(!vis[v]){
            dfs(v, id);
            dp[x] += dp[v];
        }
    }

    if(id != -1){
        if(!dp[x]){
            answer[id] = 'B';
        }
        else{
            if(dp[x] < 0){
                answer[id] = (x == E[id].se ? 'R' : 'L');
            }
            else answer[id] = (x == E[id].fi ? 'R' : 'L');
        }
    }
    return;
}

void solve()
{
    cin >> n >> m;
    FOR(i, 1, m){
        int u,v; cin >> u >> v;
        E[i] = make_pair(u, v);
        g[u].pb(i), g[v].pb(i);
    }

    FOR(i, 1, n){
        if(!tin[i]){
            Tarjan(i, -1);
        }
    }

    FOR(i, 1, m){
        int u = E[i].fi, v = E[i].se;
        if(scc[u] != scc[v]){
            u = scc[u], v = scc[v];

            adj[u].pb(i), adj[v].pb(i);
            E[i] = make_pair(u, v);
        }
        else{
            answer[i] = 'B';
        }
    }

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

    FOR(i, 1, n){
        if(!vis[i]){
            dfs(i, -1);
        }
    }

    FOR(i, 1, m){
        cout << answer[i];
    } cout << "\n";
}

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

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message (stderr)

oneway.cpp: In function 'int main()':
oneway.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...