Submission #1033264

#TimeUsernameProblemLanguageResultExecution timeMemory
1033264aufanOne-Way Streets (CEOI17_oneway)C++17
100 / 100
264 ms56604 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...