답안 #1005313

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1005313 2024-06-22T10:15:18 Z Essa2006 One-Way Streets (CEOI17_oneway) C++14
100 / 100
497 ms 63840 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)

const int inf = 1e9, lg = 17;
int n, m, p, time_;
vector<int> Vis, up, dpth, upx, upy, dis, fin, U, V;
vector<vector<int>> A, X, Y, Lca;
map<array<int, 2>, int> dir, has, go;

void pre() {
    Vis.resize(n + 1), up.resize(n + 1, inf), dpth.resize(n + 1, inf), upx.resize(n + 1, inf), upy.resize(n + 1, inf), dis.resize(n + 1), fin.resize(n + 1), U.resize(m), V.resize(m);
    A.resize(n + 1), X.resize(n + 1), Y.resize(n + 1), Lca.resize(n + 1, vector<int>(lg + 1));
}

int lca(int u, int v) {
    if (dpth[u] > dpth[v]) {
        swap(u, v);
    }

    // u

    // v

    for (int i = lg; i >= 0; i--) {
        if (dpth[Lca[v][i]] >= dpth[u]) {
            v = Lca[v][i];
        }
    }

    for (int i = lg; i >= 0; i--) {
        if (Lca[v][i] != Lca[u][i]) {
            u = Lca[u][i], v = Lca[v][i];
        }
    }
    if (u != v) {
        u = Lca[u][0];
    }
    return u;
}

void dfs(int u, int p = -1) {
    Vis[u] = 1;
    up[u] = dpth[u];
    for (auto v : A[u]) {
        if (v == p) {
            continue;
        }
        if (!Vis[v]) {
            dpth[v] = dpth[u] + 1;
            dfs(v, u);
            upx[u] = min(upx[u], upx[v]);
            upy[u] = min(upy[u], upy[v]);
        }
        up[u] = min(up[u], up[v]);
    }

    for (int i = 0; i < X[u].size(); i++) {
        upx[u] = min(upx[u], dpth[lca(u, X[u][i])]);
    }
    for (int i = 0; i < Y[u].size(); i++) {
        upy[u] = min(upy[u], dpth[lca(u, Y[u][i])]);
    }

    if (up[u] == dpth[u] && has[{u, p}] == 1) {
        assert(max(upx[u], upy[u]) >= dpth[u]);
        if (upy[u] < dpth[u]) {
            go[{u, p}] = 1;
        }
        else if (upx[u] < dpth[u]){
            go[{p, u}] = 1;
        }
    }
}

void calc(int u, int p = -1) {
    dis[u] = ++time_;
    Lca[u][0] = (p == -1 ? u : p);
    for (int i = 1; i <= lg; i++) {
        Lca[u][i] = Lca[Lca[u][i - 1]][i - 1];
    }
    for (auto v : A[u]) {
        if (!dis[v]) {
            dpth[v] = dpth[u] + 1;
            calc(v, u);
        }
    }
    fin[u] = ++time_;
}

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

    cin >> n >> m;
    pre();

    for (int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;

        A[u].push_back(v);
        A[v].push_back(u);
        dir[{u, v}] = 1;
        has[{u, v}]++;
        has[{v, u}]++;

        U[i] = u, V[i] = v;
    }

    cin >> p;
    for (int i = 0 ; i < p; i++) {
        int x, y;
        cin >> x >> y;

        X[y].push_back(x);
        Y[x].push_back(y);
    }

    for (int i = 1; i <= n; i++) {
        if (Vis[i]) {
            continue;
        }
        dpth[i] = 0;
        calc(i);
        dfs(i);
    }

    for (int i = 0; i < m; i++) {
        int u = U[i], v = V[i];
        if (!(go[{u, v}] || go[{v, u}]) || u == v || has[{u, v}] != 1) {
            cout << 'B';
            continue;
        }
        if (go[{u, v}]) {
            cout << 'R';
            continue;
        }
        cout << 'L';
    }
}

Compilation message

oneway.cpp: In function 'void dfs(int, int)':
oneway.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for (int i = 0; i < X[u].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
oneway.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for (int i = 0; i < Y[u].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 920 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 920 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 219 ms 40016 KB Output is correct
12 Correct 234 ms 43520 KB Output is correct
13 Correct 230 ms 48976 KB Output is correct
14 Correct 245 ms 55712 KB Output is correct
15 Correct 255 ms 57940 KB Output is correct
16 Correct 290 ms 55784 KB Output is correct
17 Correct 271 ms 57428 KB Output is correct
18 Correct 293 ms 55636 KB Output is correct
19 Correct 251 ms 59472 KB Output is correct
20 Correct 209 ms 46676 KB Output is correct
21 Correct 188 ms 44884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 920 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 219 ms 40016 KB Output is correct
12 Correct 234 ms 43520 KB Output is correct
13 Correct 230 ms 48976 KB Output is correct
14 Correct 245 ms 55712 KB Output is correct
15 Correct 255 ms 57940 KB Output is correct
16 Correct 290 ms 55784 KB Output is correct
17 Correct 271 ms 57428 KB Output is correct
18 Correct 293 ms 55636 KB Output is correct
19 Correct 251 ms 59472 KB Output is correct
20 Correct 209 ms 46676 KB Output is correct
21 Correct 188 ms 44884 KB Output is correct
22 Correct 497 ms 58600 KB Output is correct
23 Correct 443 ms 55632 KB Output is correct
24 Correct 441 ms 55524 KB Output is correct
25 Correct 494 ms 63840 KB Output is correct
26 Correct 471 ms 57684 KB Output is correct
27 Correct 449 ms 55692 KB Output is correct
28 Correct 103 ms 4904 KB Output is correct
29 Correct 329 ms 48508 KB Output is correct
30 Correct 302 ms 48724 KB Output is correct
31 Correct 352 ms 49420 KB Output is correct
32 Correct 329 ms 47704 KB Output is correct