Submission #585363

#TimeUsernameProblemLanguageResultExecution timeMemory
585363JomnoiOne-Way Streets (CEOI17_oneway)C++17
100 / 100
87 ms12940 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 5;
const int MAX_M = 1e5 + 5;

vector <int> graph[MAX_N];
int L[MAX_M], R[MAX_M];
bool visited[MAX_N];
bool treeEdge[MAX_M];
int qs1[MAX_N], qs2[MAX_N];
char ans[MAX_M];

void dfs(int u, int p) {
    visited[u] = true;
    for(auto i : graph[u]) {
        int v = u ^ L[i] ^ R[i];
        if(v == p) {
            continue;
        }

        if(visited[v] == false) {
            treeEdge[i] = true;
            dfs(v, u);
        }
        else {
            if(L[i] == u) {
                swap(L[i], R[i]);
            }
        }
    }
}

void dfs2(int u, int p) {
    visited[u] = false;
    for(auto i : graph[u]) {
        int v = u ^ L[i] ^ R[i];
        if(v == p or visited[v] == false) {
            continue;
        }

        dfs2(v, u);

        qs1[u] += qs1[v];
        qs2[u] += qs2[v];
        if(qs1[v] != 0 or qs2[v] == 0) {
            ans[i] = 'B';
            continue;
        }

        if(qs2[v] > 0) {
            if(v == L[i]) {
                ans[i] = 'R';
            }
            else {
                ans[i] = 'L';
            }
        }
        else {
            if(u == L[i]) {
                ans[i] = 'R';
            }
            else {
                ans[i] = 'L';
            }
        }
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    for(int i = 1; i <= m; i++) {
        cin >> L[i] >> R[i];

        graph[L[i]].push_back(i);
        graph[R[i]].push_back(i);
    }

    for(int i = 1; i <= n; i++) {
        if(visited[i] == false) {
            dfs(i, -1);
        }
    }

    for(int i = 1; i <= m; i++) {
        if(treeEdge[i] == false) {
            ans[i] = 'B';
            qs1[L[i]]++;
            qs1[R[i]]--;
        }
    }

    int p;
    cin >> p;
    
    while(p--) {
        int x, y;
        cin >> x >> y;

        qs2[x]++;
        qs2[y]--;
    }

    for(int i = 1; i <= n; i++) {
        if(visited[i] == true) {
            dfs2(i, -1);
        }
    }

    for(int i = 1; i <= m; i++) {
        cout << ans[i];
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...