Submission #1003763

# Submission time Handle Problem Language Result Execution time Memory
1003763 2024-06-20T16:55:41 Z coolboy19521 One-Way Streets (CEOI17_oneway) C++17
100 / 100
214 ms 76796 KB
#pragma GCC optimize("Ofast")
#include"bits/stdc++.h"
#define int long long

using namespace std;

const int sz = 2e5 + 10;
const int sm = 25;

vector<vector<int>> aj[sz], ajgr[sz];
int br[sz], gr[sz], dp[sz];
int in[sz], lo[sz];
int pn[sz], pe[sz];
int le[sz], ri[sz];
int up[sm][sz];
int n, m, p;
bool dr[sz];
char la[sz];
int tr;

void taj(int v, int p = 0) {
    in[v] = lo[v] = ++ tr;
    bool ps = false;
    for (auto& u : aj[v]) {
        int to = u[0], nm = u[1];
        if (to == p && !ps) {
            ps = true;
            continue;
        } else if (in[to]) {
            lo[v] = min(lo[v], in[to]);
        } else {
            taj(to, v);
            lo[v] = min(lo[v], lo[to]);
            if (lo[to] > in[v]) {
                br[nm] = true;
            }
        }
    }
}

void cmp(int v, int gn) {
    gr[v] = gn;
    for (auto& u : aj[v]) {
        int to = u[0], nm = u[1];
        if (!gr[to] && !br[nm]) {
            cmp(to, gn);
        }
    }
}

void dpt(int v, int p = 0, int d = 1) {
    up[0][v] = pn[v];
    dp[v] = d;
    for (auto& u : ajgr[v]) {
        int to = u[0], nm = u[1];
        if (to != p) {
            pn[to] = v;
            pe[to] = nm;
            dpt(to, v, d + 1);
        }
    }
}

int lca(int a, int b) {
    if (dp[a] > dp[b]) {
        swap(a, b);
    }
    int d = dp[b] - dp[a];
    for (int i = 0; i < sm; i ++) {
        if (d & (1ll << i)) {
            b = up[i][b];
        }
    }
    if (a == b) {
        return a;
    }
    for (int i = sm - 1; i >= 0; i--) {
        if (up[i][a] != up[i][b]) {
            a = up[i][a];
            b = up[i][b];
        }
    }
    return up[0][a];
}

void gdr(int a, int b, int d) {
    if (a == b) {
        return;
    }
    if (!dr[a]) {
        dr[a] = true;
        int to = pn[a];
        int nm = pe[a];
        int gna = gr[le[nm]];
        if (0 == d) {
            if (gna == a) {
                la[nm] = 'R';
            } else {
                la[nm] = 'L';
            }
        } else {
            if (gna == a) {
                la[nm] = 'L';
            } else {
                la[nm] = 'R';
            }
        }
        gdr(to, b, d);
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    cin >> n >> m;

    for (int i = 1; i <= m; i ++) {
        int a, b;
        cin >> a >> b;

        aj[a].push_back({b, i});
        aj[b].push_back({a, i});
        le[i] = a;
        ri[i] = b;
        la[i] = 'B';
    }

    for (int i = 1; i <= n; i ++) {
        if (!in[i]) {
            taj(i);
        }
    }

    int gn = 0;

    for (int i = 1; i <= n; i ++) {
        if (!gr[i]) {
            cmp(i, ++ gn);
        }
    }

    for (int i = 1; i <= m; i ++) {
        if (br[i]) {
            int gna = gr[le[i]];
            int gnb = gr[ri[i]];
            ajgr[gna].push_back({gnb, i});
            ajgr[gnb].push_back({gna, i});
        }
    }

    for (int i = 1; i <= gn; i ++) {
        if (!dp[i]) {
            dpt(i);
        }
    }

    for (int i = 1; i < sm; i ++) {
        for (int j = 1; j <= gn; j ++) {
            up[i][j] = up[i - 1][up[i - 1][j]];
        }
    }

    vector<vector<int>> ord;

    int q;
    cin >> q;

    while (q --) {
        int a, b;
        cin >> a >> b;

        a = gr[a];
        b = gr[b];
        int l = lca(a, b);
        ord.push_back({dp[l], a, b, l});
    }

    sort(begin(ord), end(ord));

    for (auto& i : ord) {
        int a = i[1];
        int b = i[2];
        int l = i[3];
        gdr(a, l, 0);
        gdr(b, l, 1);
    }

    for (int i = 1; i <= m; i ++) {
        cout << la[i];
    }

    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10096 KB Output is correct
2 Correct 5 ms 10076 KB Output is correct
3 Correct 5 ms 10076 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 6 ms 10668 KB Output is correct
6 Correct 5 ms 10032 KB Output is correct
7 Correct 6 ms 10588 KB Output is correct
8 Correct 6 ms 10628 KB Output is correct
9 Correct 5 ms 10276 KB Output is correct
10 Correct 5 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10096 KB Output is correct
2 Correct 5 ms 10076 KB Output is correct
3 Correct 5 ms 10076 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 6 ms 10668 KB Output is correct
6 Correct 5 ms 10032 KB Output is correct
7 Correct 6 ms 10588 KB Output is correct
8 Correct 6 ms 10628 KB Output is correct
9 Correct 5 ms 10276 KB Output is correct
10 Correct 5 ms 10076 KB Output is correct
11 Correct 49 ms 27484 KB Output is correct
12 Correct 53 ms 29264 KB Output is correct
13 Correct 65 ms 31632 KB Output is correct
14 Correct 81 ms 39488 KB Output is correct
15 Correct 85 ms 42764 KB Output is correct
16 Correct 108 ms 63572 KB Output is correct
17 Correct 108 ms 65360 KB Output is correct
18 Correct 107 ms 63572 KB Output is correct
19 Correct 101 ms 66544 KB Output is correct
20 Correct 53 ms 28256 KB Output is correct
21 Correct 47 ms 29008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10096 KB Output is correct
2 Correct 5 ms 10076 KB Output is correct
3 Correct 5 ms 10076 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 6 ms 10668 KB Output is correct
6 Correct 5 ms 10032 KB Output is correct
7 Correct 6 ms 10588 KB Output is correct
8 Correct 6 ms 10628 KB Output is correct
9 Correct 5 ms 10276 KB Output is correct
10 Correct 5 ms 10076 KB Output is correct
11 Correct 49 ms 27484 KB Output is correct
12 Correct 53 ms 29264 KB Output is correct
13 Correct 65 ms 31632 KB Output is correct
14 Correct 81 ms 39488 KB Output is correct
15 Correct 85 ms 42764 KB Output is correct
16 Correct 108 ms 63572 KB Output is correct
17 Correct 108 ms 65360 KB Output is correct
18 Correct 107 ms 63572 KB Output is correct
19 Correct 101 ms 66544 KB Output is correct
20 Correct 53 ms 28256 KB Output is correct
21 Correct 47 ms 29008 KB Output is correct
22 Correct 208 ms 73520 KB Output is correct
23 Correct 198 ms 71752 KB Output is correct
24 Correct 214 ms 71764 KB Output is correct
25 Correct 189 ms 76796 KB Output is correct
26 Correct 195 ms 73004 KB Output is correct
27 Correct 198 ms 71936 KB Output is correct
28 Correct 53 ms 31232 KB Output is correct
29 Correct 80 ms 36096 KB Output is correct
30 Correct 92 ms 36536 KB Output is correct
31 Correct 77 ms 36628 KB Output is correct
32 Correct 117 ms 49156 KB Output is correct