답안 #952489

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952489 2024-03-24T05:57:13 Z _callmelucian One-Way Streets (CEOI17_oneway) C++14
100 / 100
152 ms 41680 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 1e5 + 5;
int num[mn], low[mn], sz[mn], depth[mn], timeDfs;
int up[mn][21], st[mn][21], en[mn][21];
bool isBridge[mn], vis[mn];
vector<pii> adj[mn];

void dfs (int u, int preID, int d, int p) {
    num[u] = low[u] = ++timeDfs, depth[u] = d;
    vis[u] = 1, sz[u] = 1, up[u][0] = p;
    for (int i = 1; i < 17; i++)
        up[u][i] = up[up[u][i - 1]][i - 1];
    for (pii it : adj[u]) {
        int v, id; tie(v, id) = it;
        if (id == preID) continue;
        if (!vis[v]) {
            dfs(v, id, d + 1, u);
            sz[u] += sz[v], low[u] = min(low[u], low[v]);
            if (low[v] == num[v]) isBridge[id] = 1;
        }
        else low[u] = min(low[u], num[v]);
    }
}

int goUp (int a, int k) {
    for (int i = 0; i < 17; i++)
        if (k & (1 << i)) a = up[a][i];
    return a;
}

int lca (int a, int b) {
    if (depth[a] < depth[b]) swap(a, b);
    a = goUp(a, depth[a] - depth[b]);
    if (a == b) return a;
    for (int i = 16; i >= 0; i--)
        if (up[a][i] != up[b][i])
            a = up[a][i], b = up[b][i];
    return up[a][0];
}

int queryST (int u) {
    int l = num[u], r = num[u] + sz[u] - 1, p = 31 - __builtin_clz(r - l + 1);
    return min(st[l][p], st[r - (1 << p) + 1][p]);
}

int queryEN (int u) {
    int l = num[u], r = num[u] + sz[u] - 1, p = 31 - __builtin_clz(r - l + 1);
    return min(en[l][p], en[r - (1 << p) + 1][p]);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m; cin >> n >> m;
    vector<pii> edge(m);
    vector<char> ans(m);

    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        if (a != b) {
            adj[a].push_back({b, i});
            adj[b].push_back({a, i});
        }
        else ans[i] = 'B';

        edge[i] = {a, b};
    }

    for (int i = 1; i <= n; i++)
        if (!vis[i])
            dfs(i, -1, 1, 0);

    for (int i = 1; i <= n; i++)
        st[i][0] = en[i][0] = INT_MAX;

    int p; cin >> p;
    while (p--) {
        int a, b; cin >> a >> b;
        st[num[a]][0] = min(st[num[a]][0], depth[lca(a, b)]);
        en[num[b]][0] = min(en[num[b]][0], depth[lca(a, b)]);
    }
    for (int s = 1; (1 << s) <= n; s++) {
        for (int i = 1; i + (1 << s) - 1 <= n; i++) {
            int p = s - 1;
            st[i][s] = min(st[i][p], st[i + (1 << p)][p]);
            en[i][s] = min(en[i][p], en[i + (1 << p)][p]);
        }
    }

    for (int i = 0; i < m; i++) {
        int a, b; bool flp = 0; tie(a, b) = edge[i];
        if (a == b) continue;
        if (depth[a] > depth[b]) {
            swap(a, b);
            flp = 1;
        }
        if (!isBridge[i]) ans[i] = 'B';
        else {
            bool up = 0, down = 0;
            if (queryST(b) <= depth[a]) up = 1;
            else if (queryEN(b) <= depth[a]) down = 1;
            if (up == down) ans[i] = 'B';
            else {
                if (up) ans[i] = (flp ? 'R': 'L');
                else ans[i] = (flp ? 'L' : 'R');
            }
        }
    }

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

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 9048 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 9048 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 33 ms 15188 KB Output is correct
12 Correct 35 ms 22612 KB Output is correct
13 Correct 46 ms 29776 KB Output is correct
14 Correct 59 ms 36948 KB Output is correct
15 Correct 62 ms 36948 KB Output is correct
16 Correct 63 ms 34896 KB Output is correct
17 Correct 74 ms 36944 KB Output is correct
18 Correct 82 ms 34972 KB Output is correct
19 Correct 68 ms 38224 KB Output is correct
20 Correct 40 ms 28288 KB Output is correct
21 Correct 38 ms 27984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 9048 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 33 ms 15188 KB Output is correct
12 Correct 35 ms 22612 KB Output is correct
13 Correct 46 ms 29776 KB Output is correct
14 Correct 59 ms 36948 KB Output is correct
15 Correct 62 ms 36948 KB Output is correct
16 Correct 63 ms 34896 KB Output is correct
17 Correct 74 ms 36944 KB Output is correct
18 Correct 82 ms 34972 KB Output is correct
19 Correct 68 ms 38224 KB Output is correct
20 Correct 40 ms 28288 KB Output is correct
21 Correct 38 ms 27984 KB Output is correct
22 Correct 142 ms 37968 KB Output is correct
23 Correct 122 ms 36176 KB Output is correct
24 Correct 110 ms 36004 KB Output is correct
25 Correct 149 ms 41680 KB Output is correct
26 Correct 152 ms 37520 KB Output is correct
27 Correct 125 ms 36188 KB Output is correct
28 Correct 31 ms 12904 KB Output is correct
29 Correct 88 ms 28844 KB Output is correct
30 Correct 85 ms 29056 KB Output is correct
31 Correct 82 ms 29384 KB Output is correct
32 Correct 113 ms 31560 KB Output is correct