Submission #855930

# Submission time Handle Problem Language Result Execution time Memory
855930 2023-10-02T10:03:25 Z thanh913 One-Way Streets (CEOI17_oneway) C++14
100 / 100
73 ms 34048 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define ll long long

const int N = 2e5+5;

//-------------------------------
int n, m, p;
int isBridge[N];
pair<int, int> edge[N], need[N];
vector<pair<int, int>> adj[N];

vector<pair<pair<int, int>, int>> tmp;
void init() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        edge[i] = {u, v};
        adj[u].push_back({v, i});
        adj[v].push_back({u, i});
        tmp.push_back({edge[i], i});
    }
    cin >> p;
    for (int i = 1; i <= p; i++) {
        cin >> need[i].fi >> need[i].se;
    }
    sort(tmp.begin(), tmp.end());
    for (int i = 1; i < m; i++) {
        if (tmp[i].fi == tmp[i-1].fi) {
            isBridge[tmp[i].se] = isBridge[tmp[i-1].se] = -1;
        }
    }
}

int timer, num[N], low[N];
void dfs(int u, int pr) {
    num[u] = low[u] = ++timer;
    for (auto e : adj[u]) {
        int v, idx; tie(v, idx) = e;
        if (v == pr) continue;
        if (num[v]) {
            low[u] = min(low[u], num[v]);
            continue;
        }
        dfs(v, u);
        low[u] = min(low[u], low[v]);
        if (low[v] == num[v] && isBridge[idx] != -1)
            isBridge[idx] = 1;
    }
}

int cc, ins[N];
vector<pair<int, int>> tree[N];
void nen(int u) {
    ins[u] = cc;
    for (auto e : adj[u]) {
        int v, idx; tie(v, idx) = e;
        if (isBridge[idx] == 1 || ins[v]) continue;
        nen(v);
    }
}

//xu ly tren cay

int tin[N], tout[N];
bool isAnc(int u, int v) {
    return (tin[u] <= tin[v] && tout[u] >= tout[v]);
}

int up[N]; pair<int, int> cha[N];
int find(int u) {
    return ((u == up[u]) ? u : (up[u] = find(up[u])));
}

bool vs[N];
void dfs2(int u, int pr) {
    vs[u] = true;
    up[u] = u;
    tin[u] = ++timer;
    for (auto e : tree[u]) {
        if (!vs[e.fi]) {
            cha[e.fi] = {u, e.se};
            dfs2(e.fi, u);
        }
    }
    tout[u] = timer;
}

int ans[N];

void solve() {
    for (int i = 1; i <= n; i++) {
        if (!num[i]) dfs(i, i);
    }
    for (int i = 1; i <= n; i++) {
        if (ins[i]) continue;
        cc++; nen(i);
    }
    for (int i = 1; i <= m; i++) {
        if (isBridge[i] != 1) continue;
        int u, v; tie(u, v) = edge[i];
        u = ins[u]; v = ins[v];
        tree[u].push_back({v, i});
        tree[v].push_back({u, i});
    }
    timer = 0;
    for (int i = 1; i <= cc; i++) {
        if (!tin[i]) dfs2(i, i);
    }
    for (int i = 1; i <= p; i++) {
        int u, v; tie(u, v) = need[i];
        u = ins[u]; v = ins[v];
        int cur = u;
        while (!isAnc((cur = find(cur)), v)) {
            int idx, pr; tie(pr, idx) = cha[cur];
            if (make_pair(cur, pr) == make_pair(ins[edge[idx].fi], ins[edge[idx].se]))
                ans[idx] = 2;
            else
                ans[idx] = 1;
            up[cur] = up[pr];
        }

        cur = v;
        while (!isAnc((cur = find(cur)), u)) {
            int idx, pr; tie(pr, idx) = cha[cur];
            if (make_pair(cur, pr) == make_pair(ins[edge[idx].fi], ins[edge[idx].se]))
                ans[idx] = 1;
            else
                ans[idx] = 2;
            up[cur] = up[pr];
        }
    }
    for (int i = 1; i <= m; i++) {
        if (isBridge[i] == 1) {
            if (ans[i] == 0) cout << 'B';
            if (ans[i] == 1) cout << 'L';
            if (ans[i] == 2) cout << 'R';
        }
        else cout << 'B';
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    init();
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16984 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16728 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 4 ms 16832 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 4 ms 16828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16984 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16728 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 4 ms 16832 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 4 ms 16828 KB Output is correct
11 Correct 37 ms 23528 KB Output is correct
12 Correct 40 ms 24592 KB Output is correct
13 Correct 46 ms 25596 KB Output is correct
14 Correct 51 ms 26632 KB Output is correct
15 Correct 51 ms 27140 KB Output is correct
16 Correct 62 ms 28336 KB Output is correct
17 Correct 56 ms 29964 KB Output is correct
18 Correct 57 ms 28416 KB Output is correct
19 Correct 57 ms 30984 KB Output is correct
20 Correct 39 ms 23612 KB Output is correct
21 Correct 44 ms 23940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16984 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16728 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 4 ms 16832 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 4 ms 16828 KB Output is correct
11 Correct 37 ms 23528 KB Output is correct
12 Correct 40 ms 24592 KB Output is correct
13 Correct 46 ms 25596 KB Output is correct
14 Correct 51 ms 26632 KB Output is correct
15 Correct 51 ms 27140 KB Output is correct
16 Correct 62 ms 28336 KB Output is correct
17 Correct 56 ms 29964 KB Output is correct
18 Correct 57 ms 28416 KB Output is correct
19 Correct 57 ms 30984 KB Output is correct
20 Correct 39 ms 23612 KB Output is correct
21 Correct 44 ms 23940 KB Output is correct
22 Correct 71 ms 31048 KB Output is correct
23 Correct 71 ms 29444 KB Output is correct
24 Correct 70 ms 29564 KB Output is correct
25 Correct 73 ms 34048 KB Output is correct
26 Correct 73 ms 30684 KB Output is correct
27 Correct 71 ms 29440 KB Output is correct
28 Correct 36 ms 22040 KB Output is correct
29 Correct 55 ms 24316 KB Output is correct
30 Correct 51 ms 24732 KB Output is correct
31 Correct 51 ms 24832 KB Output is correct
32 Correct 57 ms 28420 KB Output is correct