Submission #1033264

# Submission time Handle Problem Language Result Execution time Memory
1033264 2024-07-24T15:32:14 Z aufan One-Way Streets (CEOI17_oneway) C++17
100 / 100
264 ms 56604 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

int32_t main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        int n, m;
        cin >> n >> m;

        vector<pair<int, int>> edg;
        vector<vector<pair<int, int>>> v(n + 1);
        for (int i = 0; i < m; i++) {
                int a, b;
                cin >> a >> b;

                edg.push_back({a, b});
                v[a].push_back({b, i});
                v[b].push_back({a, i});
        }

        int tim = 0;
        vector<int> disc(n + 1), low(n + 1), bridge(m);

        function<void(int, int, int)> dfs = [&](int x, int p, int id) {
                disc[x] = low[x] = ++tim;

                for (auto [z, i] : v[x]) {
                        if (i == id) continue;

                        if (disc[z] == 0) {
                                dfs(z, x, i);

                                low[x] = min(low[x], low[z]);
                                if (disc[x] < low[z]) {
                                        bridge[i] = 1;
                                }
                        } else {
                                low[x] = min(low[x], disc[z]);
                        }
                }
        };

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

        int nd = 0;
        vector<int> hd(n + 1);

        function<void(int)> dfs2 = [&](int x) {
                hd[x] = nd;

                for (auto [z, i] : v[x]) {
                        if (hd[z] != 0 || bridge[i]) continue;

                        dfs2(z);
                }
        };

        for (int i = 1; i <= n; i++) {
                if (hd[i] == 0) {
                        nd += 1;
                        dfs2(i);
                }
        }

        vector<vector<int>> g(nd + 1);
        for (int i = 0; i < m; i++) {
                if (bridge[i]) {
                        auto [a, b] = edg[i];

                        g[hd[a]].push_back(hd[b]);
                        g[hd[b]].push_back(hd[a]);
                }
        }
        
        for (int i = 1; i <= nd; i++) {
                sort(g[i].begin(), g[i].end());
                g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
        }

        vector<int> dep(nd + 1, -1);
        vector<vector<int>> p(nd + 1, vector<int>(22));

        function<void(int, int)> dfs3 = [&](int x, int pr) {
                p[x][0] = pr;
                dep[x] = dep[pr] + 1;
                for (int j = 1; j < 22; j++) p[x][j] = p[p[x][j - 1]][j - 1];

                for (auto z : g[x]) {
                        if (z == pr) continue;

                        dfs3(z, x);
                }
        };

        for (int i = 1; i <= nd; i++) {
                if (dep[i] == -1) {
                        dfs3(i, i);
                }
        }

        auto lca = [&](int x, int y) {
                if (dep[x] < dep[y]) swap(x, y);

                for (int j = 21; j >= 0; j--) {
                        if (dep[p[x][j]] >= dep[y]) {
                                x = p[x][j];
                        }
                }

                if (x == y) return x;

                for (int j = 21; j >= 0; j--) {
                        if (p[x][j] != p[y][j]) {
                                x = p[x][j];
                                y = p[y][j];
                        }
                }

                return p[x][0];
        };

        int q;
        cin >> q;

        vector<vector<int>> c(nd + 1, vector<int>(2)); // 0 for up, 1 for down
        for (int i = 0; i < q; i++) {
                int a, b;
                cin >> a >> b;
                a = hd[a]; b = hd[b];

                c[a][0] += 1;
                c[lca(a, b)][0] -= 1;

                c[b][1] += 1;
                c[lca(a, b)][1] -= 1;   
        }

        vector<int> vis(nd + 1);

        function<void(int, int)> dfs4 = [&](int x, int pr) {
                vis[x] = 1;

                for (auto z : g[x]) {
                        if (z == pr) continue;

                        dfs4(z, x);

                        c[x][0] += c[z][0];
                        c[x][1] += c[z][1];
                }
        };

        for (int i = 1; i <= nd; i++) {
                if (!vis[i]) {
                        dfs4(i, i);
                }
        }

        vector<char> ans(m, 'B');
        for (int i = 0; i < m; i++) {
                if (bridge[i]) {
                        auto [a, b] = edg[i];

                        if (dep[hd[a]] < dep[hd[b]]) {
                                assert(!(c[hd[b]][0] > 0 && c[hd[b]][1] > 0));
                                if (c[hd[b]][0] > 0) {
                                        ans[i] = 'L';
                                } else if (c[hd[b]][1] > 0) {
                                        ans[i] = 'R';
                                }
                        } else if (dep[hd[a]] > dep[hd[b]]) {
                                assert(!(c[hd[a]][0] > 0 && c[hd[a]][1] > 0));
                                if (c[hd[a]][0] > 0) {
                                        ans[i] = 'R';
                                } else if (c[hd[a]][1] > 0) {
                                        ans[i] = 'L';
                                }
                        }
                }
        }

        for (int i = 0; i < m; i++) cout << ans[i];
        cout << '\n';

        return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 30 ms 11716 KB Output is correct
12 Correct 31 ms 13516 KB Output is correct
13 Correct 42 ms 16580 KB Output is correct
14 Correct 59 ms 25796 KB Output is correct
15 Correct 66 ms 29780 KB Output is correct
16 Correct 99 ms 48064 KB Output is correct
17 Correct 101 ms 50648 KB Output is correct
18 Correct 101 ms 48064 KB Output is correct
19 Correct 110 ms 52396 KB Output is correct
20 Correct 32 ms 13248 KB Output is correct
21 Correct 29 ms 12996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 30 ms 11716 KB Output is correct
12 Correct 31 ms 13516 KB Output is correct
13 Correct 42 ms 16580 KB Output is correct
14 Correct 59 ms 25796 KB Output is correct
15 Correct 66 ms 29780 KB Output is correct
16 Correct 99 ms 48064 KB Output is correct
17 Correct 101 ms 50648 KB Output is correct
18 Correct 101 ms 48064 KB Output is correct
19 Correct 110 ms 52396 KB Output is correct
20 Correct 32 ms 13248 KB Output is correct
21 Correct 29 ms 12996 KB Output is correct
22 Correct 264 ms 51652 KB Output is correct
23 Correct 211 ms 49208 KB Output is correct
24 Correct 175 ms 49356 KB Output is correct
25 Correct 228 ms 56604 KB Output is correct
26 Correct 224 ms 51148 KB Output is correct
27 Correct 190 ms 49256 KB Output is correct
28 Correct 34 ms 8124 KB Output is correct
29 Correct 46 ms 13760 KB Output is correct
30 Correct 58 ms 13860 KB Output is correct
31 Correct 66 ms 14272 KB Output is correct
32 Correct 122 ms 27516 KB Output is correct