답안 #1040122

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1040122 2024-07-31T16:41:58 Z Sharky Jail (JOI22_jail) C++17
77 / 100
5000 ms 397144 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 120008;
const int LG = 17;

int n, m, sz[N], dep[N], node = 0, pr[N][LG], rt[N], tin[N], pa[N], timer = 0, leaf[N * 3], ls[N * 10], rs[N * 10], st[2], vis[N * 10], cyc = 0, col[N * 10];
vector<int> adj[N], g[N * 10];

void dfs(int u) {
    sz[u] = 1;
    for (int i = 1; i < LG; i++) pr[u][i] = pr[pr[u][i - 1]][i - 1];
    for (auto& v : adj[u]) {
        if (v == pr[u][0]) continue;
        pr[v][0] = u;
        dep[v] = dep[u] + 1;
        pa[v] = u;
        dfs(v);
        if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
        sz[u] += sz[v];
    }
}

void dfs_hld(int u) {
    tin[u] = ++timer;
    for (int v : adj[u]) {
        if (v == pr[u][0]) continue;
        rt[v] = (v == adj[u][0] ? rt[u] : v);
        dfs_hld(v);
    }
}

int jump(int s, int di) {
    for (int i = LG - 1; i >= 0; i--) 
        if (di & (1 << i)) s = pr[s][i];
    return s;
}

int lca(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    v = jump(v, dep[v] - dep[u]);
    if (u == v) return u;
    for (int i = LG - 1; i >= 0; i--) {
        int ut = pr[u][i], vt = pr[v][i];
        if (ut != vt) u = ut, v = vt;
    }
    return pr[u][0];
}

void build(int l, int r, int id, bool up) {
    if (l == r) {
        leaf[l] = id;
        return;
    }
    int mid = (l + r) / 2;
    if (l != mid || !up) {
        ls[id] = ++node;
        build(l, mid, ls[id], up);
    } else ls[id] = leaf[l];
    if (mid + 1 != r || !up) {
        rs[id] = ++node;
        build(mid + 1, r, rs[id], up);
    } else rs[id] = leaf[r];
    if (!up) {
        g[id].push_back(ls[id]);
        g[id].push_back(rs[id]);
    } else {
        g[ls[id]].push_back(id);
        g[rs[id]].push_back(id);
    }
}

void update(int ql, int qr, int v, int l, int r, int id, bool up) {
    if (qr < l || r < ql) return;
    if (ql <= l && r <= qr) {
        if (!up) {
            // cout << "edge: " << v << "-->" << '[' << l << ", " << r << ']' << "\n";
            g[leaf[v]].push_back(id);
        } else {
            // cout << "edge: " << '[' << l << ", " << r << ']' << " --> " << v << "\n";
            g[id].push_back(leaf[v]);
        }
        return;
    }
    int mid = (l + r) / 2;
    update(ql, qr, v, l, mid, ls[id], up);
    update(ql, qr, v, mid + 1, r, rs[id], up);
}

void pti(int x, int l, int r) {
    update(l, r, x, 1, 2 * n + m, st[0], 0);
}

void itp(int x, int l, int r) {
    update(l, r, x, 1, 2 * n + m, st[1], 1);
}

void path(int x, int u, int v, bool b) {
    // cout << x << " " << u << " " << v << " " << b << "\n";
    for (; rt[u] != rt[v]; v = pa[rt[v]]) {
        if (dep[rt[u]] > dep[rt[v]]) swap(u, v);
        if (!b) pti(x + 2 * n, tin[rt[v]] + n, tin[v] + n);
        else itp(x + 2 * n, tin[rt[v]], tin[v]);
    }
    if (dep[u] > dep[v]) swap(u, v);
    if (!b) pti(x + 2 * n, tin[u] + n, tin[v] + n);
    else itp(x + 2 * n, tin[u], tin[v]);
}

bool DFS(int u) {
    col[u] = 1;
    for (int v : g[u]) {
        if (!col[v]) {
            if (DFS(v)) {
                cyc = 1;
                return true;
            }
        } else if (col[v] == 1) {
            cyc = 1;
            return true;
        }
    }
    col[u] = 2;
    return false;
}

void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cin >> m;
    rt[1] = pr[1][0] = 1;
    dfs(1); dfs_hld(1);
    st[0] = ++node;
    build(1, 2 * n + m, node, 0);
    st[1] = ++node;
    build(1, 2 * n + m, node, 1);
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        int x = lca(u, v);
        g[leaf[n * 2 + i]].push_back(leaf[tin[u]]);
        g[leaf[tin[v] + n]].push_back(leaf[n * 2 + i]);
        // cout << "edge: " << n * 2 + i << " --> " << tin[u] << '\n';
        // cout << "edge: " << tin[v] + n << " --> " << n * 2 + i << '\n';
        int d = max(dep[u], dep[v]) - dep[x] - 1;
        if (u != x) {
            path(i, pa[u], v, 1);
        } else {
            path(i, jump(v, d), v, 1);
        }
        if (v != x) {
            path(i, u, pa[v], 0);
        } else {
            path(i, u, jump(u, d), 0);
        }
    }
    // for (int i = 1; i <= node; i++) for (int j : g[i]) cout << i << " --> " << j << '\n';
    for (int i = 1; i <= node; i++) if (!col[i] && DFS(i)) break;
    cout << (cyc ? "No" : "Yes") << '\n';
    for (int i = 1; i <= n; i++) {
        sz[i] = dep[i] = rt[i] = tin[i] = pa[i] = 0;
        adj[i].clear();
        for (int j = 0; j < LG; j++) pr[i][j] = 0;
    }
    for (int i = 1; i <= n * 3; i++) leaf[i] = 0;
    for (int i = 1; i <= node; i++) {
        g[i].clear();
        ls[i] = rt[i] = col[i] = vis[i] = 0;
    }
    timer = cyc = node = st[0] = st[1] = 0;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int q;
    cin >> q;
    while (q--) solve();   
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41560 KB Output is correct
2 Correct 6 ms 41564 KB Output is correct
3 Correct 5 ms 43612 KB Output is correct
4 Correct 15 ms 44016 KB Output is correct
5 Correct 26 ms 44484 KB Output is correct
6 Correct 7 ms 41564 KB Output is correct
7 Correct 6 ms 41620 KB Output is correct
8 Correct 7 ms 41816 KB Output is correct
9 Correct 35 ms 44880 KB Output is correct
10 Correct 84 ms 97364 KB Output is correct
11 Correct 13 ms 41820 KB Output is correct
12 Correct 39 ms 42480 KB Output is correct
13 Correct 144 ms 106580 KB Output is correct
14 Correct 155 ms 107340 KB Output is correct
15 Correct 254 ms 112308 KB Output is correct
16 Correct 465 ms 132688 KB Output is correct
17 Correct 173 ms 116820 KB Output is correct
18 Correct 172 ms 111184 KB Output is correct
19 Correct 184 ms 115796 KB Output is correct
20 Correct 162 ms 115424 KB Output is correct
21 Correct 170 ms 116464 KB Output is correct
22 Correct 113 ms 100688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 31472 KB Output is correct
2 Correct 16 ms 31320 KB Output is correct
3 Correct 12 ms 31580 KB Output is correct
4 Correct 12 ms 31436 KB Output is correct
5 Correct 12 ms 31652 KB Output is correct
6 Correct 13 ms 31576 KB Output is correct
7 Correct 12 ms 31580 KB Output is correct
8 Correct 12 ms 31580 KB Output is correct
9 Correct 12 ms 31580 KB Output is correct
10 Correct 12 ms 31484 KB Output is correct
11 Correct 11 ms 31668 KB Output is correct
12 Correct 12 ms 31580 KB Output is correct
13 Correct 14 ms 31580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 31472 KB Output is correct
2 Correct 16 ms 31320 KB Output is correct
3 Correct 12 ms 31580 KB Output is correct
4 Correct 12 ms 31436 KB Output is correct
5 Correct 12 ms 31652 KB Output is correct
6 Correct 13 ms 31576 KB Output is correct
7 Correct 12 ms 31580 KB Output is correct
8 Correct 12 ms 31580 KB Output is correct
9 Correct 12 ms 31580 KB Output is correct
10 Correct 12 ms 31484 KB Output is correct
11 Correct 11 ms 31668 KB Output is correct
12 Correct 12 ms 31580 KB Output is correct
13 Correct 14 ms 31580 KB Output is correct
14 Correct 11 ms 31324 KB Output is correct
15 Correct 13 ms 31492 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 16 ms 31580 KB Output is correct
18 Correct 12 ms 31488 KB Output is correct
19 Correct 6 ms 39652 KB Output is correct
20 Correct 7 ms 41560 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 43848 KB Output is correct
23 Correct 12 ms 31324 KB Output is correct
24 Correct 12 ms 31580 KB Output is correct
25 Correct 14 ms 31520 KB Output is correct
26 Correct 16 ms 31520 KB Output is correct
27 Correct 13 ms 31580 KB Output is correct
28 Correct 11 ms 31324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 31472 KB Output is correct
2 Correct 16 ms 31320 KB Output is correct
3 Correct 12 ms 31580 KB Output is correct
4 Correct 12 ms 31436 KB Output is correct
5 Correct 12 ms 31652 KB Output is correct
6 Correct 13 ms 31576 KB Output is correct
7 Correct 12 ms 31580 KB Output is correct
8 Correct 12 ms 31580 KB Output is correct
9 Correct 12 ms 31580 KB Output is correct
10 Correct 12 ms 31484 KB Output is correct
11 Correct 11 ms 31668 KB Output is correct
12 Correct 12 ms 31580 KB Output is correct
13 Correct 14 ms 31580 KB Output is correct
14 Correct 11 ms 31324 KB Output is correct
15 Correct 13 ms 31492 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 16 ms 31580 KB Output is correct
18 Correct 12 ms 31488 KB Output is correct
19 Correct 6 ms 39652 KB Output is correct
20 Correct 7 ms 41560 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 43848 KB Output is correct
23 Correct 12 ms 31324 KB Output is correct
24 Correct 12 ms 31580 KB Output is correct
25 Correct 14 ms 31520 KB Output is correct
26 Correct 16 ms 31520 KB Output is correct
27 Correct 13 ms 31580 KB Output is correct
28 Correct 11 ms 31324 KB Output is correct
29 Correct 15 ms 31576 KB Output is correct
30 Correct 13 ms 31588 KB Output is correct
31 Correct 20 ms 31580 KB Output is correct
32 Correct 20 ms 31580 KB Output is correct
33 Correct 12 ms 31576 KB Output is correct
34 Correct 15 ms 31580 KB Output is correct
35 Correct 19 ms 31752 KB Output is correct
36 Correct 20 ms 31576 KB Output is correct
37 Correct 14 ms 31580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 31472 KB Output is correct
2 Correct 16 ms 31320 KB Output is correct
3 Correct 12 ms 31580 KB Output is correct
4 Correct 12 ms 31436 KB Output is correct
5 Correct 12 ms 31652 KB Output is correct
6 Correct 13 ms 31576 KB Output is correct
7 Correct 12 ms 31580 KB Output is correct
8 Correct 12 ms 31580 KB Output is correct
9 Correct 12 ms 31580 KB Output is correct
10 Correct 12 ms 31484 KB Output is correct
11 Correct 11 ms 31668 KB Output is correct
12 Correct 12 ms 31580 KB Output is correct
13 Correct 14 ms 31580 KB Output is correct
14 Correct 11 ms 31324 KB Output is correct
15 Correct 13 ms 31492 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 16 ms 31580 KB Output is correct
18 Correct 12 ms 31488 KB Output is correct
19 Correct 6 ms 39652 KB Output is correct
20 Correct 7 ms 41560 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 9 ms 43848 KB Output is correct
23 Correct 12 ms 31324 KB Output is correct
24 Correct 12 ms 31580 KB Output is correct
25 Correct 14 ms 31520 KB Output is correct
26 Correct 16 ms 31520 KB Output is correct
27 Correct 13 ms 31580 KB Output is correct
28 Correct 11 ms 31324 KB Output is correct
29 Correct 15 ms 31576 KB Output is correct
30 Correct 13 ms 31588 KB Output is correct
31 Correct 20 ms 31580 KB Output is correct
32 Correct 20 ms 31580 KB Output is correct
33 Correct 12 ms 31576 KB Output is correct
34 Correct 15 ms 31580 KB Output is correct
35 Correct 19 ms 31752 KB Output is correct
36 Correct 20 ms 31576 KB Output is correct
37 Correct 14 ms 31580 KB Output is correct
38 Correct 41 ms 35920 KB Output is correct
39 Correct 91 ms 91732 KB Output is correct
40 Correct 281 ms 53844 KB Output is correct
41 Correct 74 ms 35664 KB Output is correct
42 Correct 80 ms 35792 KB Output is correct
43 Correct 39 ms 35436 KB Output is correct
44 Correct 31 ms 32348 KB Output is correct
45 Correct 122 ms 82516 KB Output is correct
46 Correct 167 ms 82688 KB Output is correct
47 Correct 727 ms 126544 KB Output is correct
48 Correct 743 ms 126776 KB Output is correct
49 Correct 157 ms 85528 KB Output is correct
50 Correct 212 ms 85548 KB Output is correct
51 Correct 89 ms 83792 KB Output is correct
52 Correct 77 ms 83792 KB Output is correct
53 Correct 22 ms 44120 KB Output is correct
54 Correct 98 ms 87904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 33372 KB Output is correct
2 Correct 10 ms 31500 KB Output is correct
3 Correct 11 ms 31320 KB Output is correct
4 Correct 11 ms 31324 KB Output is correct
5 Correct 16 ms 31580 KB Output is correct
6 Correct 14 ms 31480 KB Output is correct
7 Correct 12 ms 31540 KB Output is correct
8 Correct 12 ms 31320 KB Output is correct
9 Correct 11 ms 31492 KB Output is correct
10 Correct 11 ms 31524 KB Output is correct
11 Correct 11 ms 31324 KB Output is correct
12 Correct 16 ms 31568 KB Output is correct
13 Correct 42 ms 32160 KB Output is correct
14 Correct 61 ms 32340 KB Output is correct
15 Correct 67 ms 32236 KB Output is correct
16 Correct 188 ms 85200 KB Output is correct
17 Correct 573 ms 101016 KB Output is correct
18 Correct 1002 ms 120128 KB Output is correct
19 Correct 313 ms 88676 KB Output is correct
20 Correct 280 ms 88804 KB Output is correct
21 Correct 317 ms 88916 KB Output is correct
22 Correct 583 ms 100388 KB Output is correct
23 Correct 418 ms 101068 KB Output is correct
24 Correct 503 ms 99772 KB Output is correct
25 Correct 436 ms 100096 KB Output is correct
26 Correct 473 ms 100444 KB Output is correct
27 Correct 438 ms 104264 KB Output is correct
28 Correct 412 ms 112828 KB Output is correct
29 Correct 371 ms 103780 KB Output is correct
30 Correct 346 ms 97232 KB Output is correct
31 Correct 289 ms 104652 KB Output is correct
32 Correct 331 ms 97488 KB Output is correct
33 Correct 319 ms 100468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41560 KB Output is correct
2 Correct 6 ms 41564 KB Output is correct
3 Correct 5 ms 43612 KB Output is correct
4 Correct 15 ms 44016 KB Output is correct
5 Correct 26 ms 44484 KB Output is correct
6 Correct 7 ms 41564 KB Output is correct
7 Correct 6 ms 41620 KB Output is correct
8 Correct 7 ms 41816 KB Output is correct
9 Correct 35 ms 44880 KB Output is correct
10 Correct 84 ms 97364 KB Output is correct
11 Correct 13 ms 41820 KB Output is correct
12 Correct 39 ms 42480 KB Output is correct
13 Correct 144 ms 106580 KB Output is correct
14 Correct 155 ms 107340 KB Output is correct
15 Correct 254 ms 112308 KB Output is correct
16 Correct 465 ms 132688 KB Output is correct
17 Correct 173 ms 116820 KB Output is correct
18 Correct 172 ms 111184 KB Output is correct
19 Correct 184 ms 115796 KB Output is correct
20 Correct 162 ms 115424 KB Output is correct
21 Correct 170 ms 116464 KB Output is correct
22 Correct 113 ms 100688 KB Output is correct
23 Correct 11 ms 31472 KB Output is correct
24 Correct 16 ms 31320 KB Output is correct
25 Correct 12 ms 31580 KB Output is correct
26 Correct 12 ms 31436 KB Output is correct
27 Correct 12 ms 31652 KB Output is correct
28 Correct 13 ms 31576 KB Output is correct
29 Correct 12 ms 31580 KB Output is correct
30 Correct 12 ms 31580 KB Output is correct
31 Correct 12 ms 31580 KB Output is correct
32 Correct 12 ms 31484 KB Output is correct
33 Correct 11 ms 31668 KB Output is correct
34 Correct 12 ms 31580 KB Output is correct
35 Correct 14 ms 31580 KB Output is correct
36 Correct 11 ms 31324 KB Output is correct
37 Correct 13 ms 31492 KB Output is correct
38 Correct 12 ms 31580 KB Output is correct
39 Correct 16 ms 31580 KB Output is correct
40 Correct 12 ms 31488 KB Output is correct
41 Correct 6 ms 39652 KB Output is correct
42 Correct 7 ms 41560 KB Output is correct
43 Correct 8 ms 41564 KB Output is correct
44 Correct 9 ms 43848 KB Output is correct
45 Correct 12 ms 31324 KB Output is correct
46 Correct 12 ms 31580 KB Output is correct
47 Correct 14 ms 31520 KB Output is correct
48 Correct 16 ms 31520 KB Output is correct
49 Correct 13 ms 31580 KB Output is correct
50 Correct 11 ms 31324 KB Output is correct
51 Correct 15 ms 31576 KB Output is correct
52 Correct 13 ms 31588 KB Output is correct
53 Correct 20 ms 31580 KB Output is correct
54 Correct 20 ms 31580 KB Output is correct
55 Correct 12 ms 31576 KB Output is correct
56 Correct 15 ms 31580 KB Output is correct
57 Correct 19 ms 31752 KB Output is correct
58 Correct 20 ms 31576 KB Output is correct
59 Correct 14 ms 31580 KB Output is correct
60 Correct 41 ms 35920 KB Output is correct
61 Correct 91 ms 91732 KB Output is correct
62 Correct 281 ms 53844 KB Output is correct
63 Correct 74 ms 35664 KB Output is correct
64 Correct 80 ms 35792 KB Output is correct
65 Correct 39 ms 35436 KB Output is correct
66 Correct 31 ms 32348 KB Output is correct
67 Correct 122 ms 82516 KB Output is correct
68 Correct 167 ms 82688 KB Output is correct
69 Correct 727 ms 126544 KB Output is correct
70 Correct 743 ms 126776 KB Output is correct
71 Correct 157 ms 85528 KB Output is correct
72 Correct 212 ms 85548 KB Output is correct
73 Correct 89 ms 83792 KB Output is correct
74 Correct 77 ms 83792 KB Output is correct
75 Correct 22 ms 44120 KB Output is correct
76 Correct 98 ms 87904 KB Output is correct
77 Correct 11 ms 33372 KB Output is correct
78 Correct 10 ms 31500 KB Output is correct
79 Correct 11 ms 31320 KB Output is correct
80 Correct 11 ms 31324 KB Output is correct
81 Correct 16 ms 31580 KB Output is correct
82 Correct 14 ms 31480 KB Output is correct
83 Correct 12 ms 31540 KB Output is correct
84 Correct 12 ms 31320 KB Output is correct
85 Correct 11 ms 31492 KB Output is correct
86 Correct 11 ms 31524 KB Output is correct
87 Correct 11 ms 31324 KB Output is correct
88 Correct 16 ms 31568 KB Output is correct
89 Correct 42 ms 32160 KB Output is correct
90 Correct 61 ms 32340 KB Output is correct
91 Correct 67 ms 32236 KB Output is correct
92 Correct 188 ms 85200 KB Output is correct
93 Correct 573 ms 101016 KB Output is correct
94 Correct 1002 ms 120128 KB Output is correct
95 Correct 313 ms 88676 KB Output is correct
96 Correct 280 ms 88804 KB Output is correct
97 Correct 317 ms 88916 KB Output is correct
98 Correct 583 ms 100388 KB Output is correct
99 Correct 418 ms 101068 KB Output is correct
100 Correct 503 ms 99772 KB Output is correct
101 Correct 436 ms 100096 KB Output is correct
102 Correct 473 ms 100444 KB Output is correct
103 Correct 438 ms 104264 KB Output is correct
104 Correct 412 ms 112828 KB Output is correct
105 Correct 371 ms 103780 KB Output is correct
106 Correct 346 ms 97232 KB Output is correct
107 Correct 289 ms 104652 KB Output is correct
108 Correct 331 ms 97488 KB Output is correct
109 Correct 319 ms 100468 KB Output is correct
110 Correct 58 ms 34680 KB Output is correct
111 Correct 29 ms 34184 KB Output is correct
112 Execution timed out 5086 ms 397144 KB Time limit exceeded
113 Halted 0 ms 0 KB -