답안 #585064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585064 2022-06-28T09:38:12 Z tengiz05 One-Way Streets (CEOI17_oneway) C++17
60 / 100
990 ms 262144 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);
                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

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 9 ms 2004 KB Output is correct
6 Correct 2 ms 456 KB Output is correct
7 Correct 5 ms 1620 KB Output is correct
8 Correct 5 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 4 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 9 ms 2004 KB Output is correct
6 Correct 2 ms 456 KB Output is correct
7 Correct 5 ms 1620 KB Output is correct
8 Correct 5 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 4 ms 596 KB Output is correct
11 Correct 135 ms 16996 KB Output is correct
12 Correct 151 ms 19660 KB Output is correct
13 Correct 178 ms 23060 KB Output is correct
14 Correct 240 ms 28852 KB Output is correct
15 Correct 289 ms 31336 KB Output is correct
16 Correct 454 ms 40932 KB Output is correct
17 Correct 663 ms 138672 KB Output is correct
18 Correct 357 ms 41024 KB Output is correct
19 Correct 648 ms 142116 KB Output is correct
20 Correct 141 ms 21396 KB Output is correct
21 Correct 128 ms 22804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 9 ms 2004 KB Output is correct
6 Correct 2 ms 456 KB Output is correct
7 Correct 5 ms 1620 KB Output is correct
8 Correct 5 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 4 ms 596 KB Output is correct
11 Correct 135 ms 16996 KB Output is correct
12 Correct 151 ms 19660 KB Output is correct
13 Correct 178 ms 23060 KB Output is correct
14 Correct 240 ms 28852 KB Output is correct
15 Correct 289 ms 31336 KB Output is correct
16 Correct 454 ms 40932 KB Output is correct
17 Correct 663 ms 138672 KB Output is correct
18 Correct 357 ms 41024 KB Output is correct
19 Correct 648 ms 142116 KB Output is correct
20 Correct 141 ms 21396 KB Output is correct
21 Correct 128 ms 22804 KB Output is correct
22 Runtime error 990 ms 262144 KB Execution killed with signal 9
23 Halted 0 ms 0 KB -