Submission #904082

# Submission time Handle Problem Language Result Execution time Memory
904082 2024-01-11T19:54:24 Z selmahbn One-Way Streets (CEOI17_oneway) C++17
30 / 100
394 ms 262144 KB
#include <bits/stdc++.h>

using namespace std;

#define pii pair<int, int>

const int INF = numeric_limits<int>::max() / 2;

vector<int> num, low, comp, used;
stack<int> s;
int dfs_count = 0;
int comp_count = 0;

vector<vector<int>> dir;
vector<int> in;
vector<int> out;

void tarjan_dfs(int current, int parent, vector<vector<pii>>& adj) {
    num[current] = low[current] = dfs_count;
    dfs_count++;
    s.push(current);
    for (pii neigh : adj[current]) {
        if (used[neigh.second] == 1) continue;
        used[neigh.second] = 1;
        if (num[neigh.first] == INF) {
            tarjan_dfs(neigh.first, current, adj);
            low[current] = min(low[current], low[neigh.first]);
        } else low[current] = min(low[current], num[neigh.first]);
    }
    if (num[current] == low[current]) {
        while (s.top() != current) {
            int top = s.top();
            s.pop();
            comp[top] = comp_count;
        }
        s.pop();
        comp[current] = comp_count;
        comp_count++;
    }
}

int dir_dfs(int current, vector<int>& visited, vector<vector<int>>& adj) {
    visited[current] = 1;
    int incoming = in[current] - out[current];
    for (int neigh : adj[current]) {
        if (visited[neigh] == 0) {
            int d = dir_dfs(neigh, visited, adj);
            if (d < 0) {
                dir[current][neigh] = -1;
                dir[neigh][current] = 1;
            } else if (d > 0) {
                dir[current][neigh] = 1;
                dir[neigh][current] = -1;
            }
            incoming += d;
        }
    }
    return incoming;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int n, m;
    cin >> n >> m;
    vector<vector<pii>> adj(n);
    vector<pii> streets;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(make_pair(b, i));
        adj[b].push_back(make_pair(a, i));
        streets.push_back(make_pair(a, b));
    }
    num = vector<int>(n, INF);
    low = vector<int>(n);
    comp = vector<int>(n);
    used = vector<int>(m, 0);
    for (int i = 0; i < n; i++) {
        if (num[i] == INF) {
            tarjan_dfs(i, i, adj);
        }
    }
    vector<vector<int>> cadj(comp_count);
    for (pii s : streets) {
        if (comp[s.first] != comp[s.second]) {
            cadj[comp[s.first]].push_back(comp[s.second]);
            cadj[comp[s.second]].push_back(comp[s.first]);
        }
    }
    in = vector<int>(comp_count, 0);
    out = vector<int>(comp_count, 0);
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        if (comp[a] != comp[b]) {
            out[comp[a]]++;
            in[comp[b]]++;
        }
    }
    vector<int> visited(comp_count, 0);
    dir = vector<vector<int>>(comp_count, vector<int>(comp_count, 0));
    for (int i = 0; i < comp_count; i++) {
        if (visited[i] == 0) dir_dfs(i, visited, cadj);
    }
    for (pii s : streets) {
        if (comp[s.first] == comp[s.second] || dir[comp[s.first]][comp[s.second]] == 0) cout << 'B';
        else if (dir[comp[s.first]][comp[s.second]] == 1) cout << 'R';
        else cout << 'L';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 28 ms 7376 KB Output is correct
12 Correct 31 ms 8904 KB Output is correct
13 Correct 78 ms 100652 KB Output is correct
14 Runtime error 394 ms 262144 KB Execution killed with signal 9
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 28 ms 7376 KB Output is correct
12 Correct 31 ms 8904 KB Output is correct
13 Correct 78 ms 100652 KB Output is correct
14 Runtime error 394 ms 262144 KB Execution killed with signal 9
15 Halted 0 ms 0 KB -