Submission #585066

# Submission time Handle Problem Language Result Execution time Memory
585066 2022-06-28T09:38:55 Z tengiz05 One-Way Streets (CEOI17_oneway) C++17
100 / 100
583 ms 86604 KB
#include <bits/stdc++.h>

using i64 = long long;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m;
    cin >> n >> m;
    
    vector<vector<pair<int, int>>> adj(n);
    vector<vector<pair<int, int>>> ADJ(n);
    
    map<pair<int, int>, int> id, ID;
    
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        adj[u].emplace_back(v, i);
        adj[v].emplace_back(u, i);
        ADJ[u].emplace_back(v, 0);
        ADJ[v].emplace_back(u, 1);
        ID[minmax(u, v)] = i;
    }
    
    vector<int> in(n), f(n), c(n);
    
    int timeStamp = 0;
    
    vector<bool> vis(n);
    stack<int> st;
    int S = 0;
    function<void(int, int)> dfs = [&](int u, int p) {
        in[u] = f[u] = timeStamp++;
        vis[u] = true;
        st.push(u);
        for (auto [v, i] : adj[u]) {
            if (!vis[v]) {
                dfs(v, i);
                f[u] = min(f[u], f[v]);
            } else if (i != p) {
                f[u] = min(f[u], f[v]);
            }
        }
        if (f[u] == in[u]) {
            int x;
            do {
                x = st.top();
                st.pop();
                c[x] = S;
            } while (x != u);
            S++;
        }
    };
    
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            dfs(i, -1);
        }
    }
    
    // for (int i = 0; i < n; i++) {
        // cout << c[i] << " \n"[i == n - 1];
    // }
    
    vector<vector<pair<int, int>>> e(n);
    for (int u = 0; u < n; u++) {
        for (auto [v, x] : ADJ[u]) {
            if (c[u] != c[v]) {
                e[c[u]].emplace_back(c[v], x);
                id[minmax(c[u], c[v])] = ID[minmax(u, v)];
                // cerr << c[u] << " : " << c[v]  << "\n";
            }
        }
    }
    
    vector<vector<int>> ev(n);
    
    int p;
    cin >> p;
    
    vector<int> U(p + 1), V(p + 1);
    
    for (int i = 1; i <= p; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        if (c[u] == c[v])
            continue;
        ev[c[u]].push_back(i);
        ev[c[v]].push_back(-i);
    }
    
    vector<int> ans(m, -1);
    
    vis.assign(n, 0);
    
    set<int> s[n];
    
    function<void(int, int, int)> dfs2 = [&](int u, int p, int d) {
        vis[u] = true;
        for (int x : ev[u]) {
            s[u].insert(x);
        }
        for (auto [v, x] : e[u]) {
            if (vis[v]) assert(v == p);
            if (!vis[v]) {
                dfs2(v, u, x);
                if (s[v].size() > s[u].size()) swap(s[u], s[v]);
                for (int g : s[v]) {
                    if (s[u].count(-g)) {
                        s[u].erase(-g);
                    } else {
                        s[u].insert(g);
                    }
                }
            }
        }
        if (p != -1 && !s[u].empty()) {
            ans[id[minmax(p, u)]] = d ^ (*s[u].begin() > 0);
        }
    };
    
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            dfs2(i, -1, -1);
        }
    }
    
    for (int i = 0; i < m; i++) {
        if (ans[i] == 0) {
            cout << "R";
        } else if (ans[i] == 1) {
            cout << "L";
        } else {
            cout << "B";
        }
    }
    
    return 0;
}
/*

5 6
1 2
1 2
4 3
2 3
1 3
5 1
2
4 5
1 3

3 2
1 2
2 3

2
3 1
3 1

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 2 ms 712 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 2 ms 712 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 452 KB Output is correct
11 Correct 115 ms 16320 KB Output is correct
12 Correct 128 ms 18924 KB Output is correct
13 Correct 155 ms 22348 KB Output is correct
14 Correct 214 ms 27984 KB Output is correct
15 Correct 223 ms 30672 KB Output is correct
16 Correct 381 ms 40172 KB Output is correct
17 Correct 318 ms 43352 KB Output is correct
18 Correct 358 ms 40284 KB Output is correct
19 Correct 325 ms 47220 KB Output is correct
20 Correct 123 ms 20704 KB Output is correct
21 Correct 105 ms 20452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 2 ms 712 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 452 KB Output is correct
11 Correct 115 ms 16320 KB Output is correct
12 Correct 128 ms 18924 KB Output is correct
13 Correct 155 ms 22348 KB Output is correct
14 Correct 214 ms 27984 KB Output is correct
15 Correct 223 ms 30672 KB Output is correct
16 Correct 381 ms 40172 KB Output is correct
17 Correct 318 ms 43352 KB Output is correct
18 Correct 358 ms 40284 KB Output is correct
19 Correct 325 ms 47220 KB Output is correct
20 Correct 123 ms 20704 KB Output is correct
21 Correct 105 ms 20452 KB Output is correct
22 Correct 499 ms 71688 KB Output is correct
23 Correct 516 ms 85068 KB Output is correct
24 Correct 583 ms 86604 KB Output is correct
25 Correct 483 ms 72708 KB Output is correct
26 Correct 497 ms 67516 KB Output is correct
27 Correct 529 ms 77056 KB Output is correct
28 Correct 42 ms 7172 KB Output is correct
29 Correct 196 ms 32692 KB Output is correct
30 Correct 223 ms 35384 KB Output is correct
31 Correct 156 ms 27076 KB Output is correct
32 Correct 260 ms 45796 KB Output is correct