Submission #941651

# Submission time Handle Problem Language Result Execution time Memory
941651 2024-03-09T08:37:26 Z alextodoran Jail (JOI22_jail) C++17
100 / 100
849 ms 300576 KB
/**
 _  _   __  _ _ _  _  _ _
 |a  ||t  ||o    d | |o  |
| __    _| | _ | __|  _ |
| __ |/_  | __  /__\ / _\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 120000;
const int M_MAX = N_MAX;
const int LOG_N = 17;
const int V_MAX = M_MAX + N_MAX * LOG_N * 2;

int Q;

int N;
vector <int> adj[N_MAX + 2];
int M;
int start[M_MAX + 2], finish[M_MAX + 2];

int parent[N_MAX + 2];
int depth[N_MAX + 2];

void dfs (int u) {
    for (int v : adj[u]) {
        if (v != parent[u]) {
            parent[v] = u;
            depth[v] = depth[u] + 1;
            dfs(v);
        }
    }
}

int start_id[N_MAX + 2], finish_id[N_MAX + 2];

int V;
vector <int> out[V_MAX + 2];
int deg[V_MAX + 2];

void add_edge (int u, int v) {
    out[u].push_back(v);
    deg[v]++;
}

int anc[N_MAX + 2][LOG_N];
int seg_out[N_MAX + 2][LOG_N];
int seg_in[N_MAX + 2][LOG_N];

void build () {
    for (int u = 1; u <= N; u++) {
        anc[u][0] = parent[u];
    }
    for (int k = 1; k < LOG_N; k++) {
        for (int u = 1; u <= N; u++) {
            anc[u][k] = anc[anc[u][k - 1]][k - 1];
        }
    }
    V = M;
    for (int u = 1; u <= N; u++) {
        seg_out[u][0] = ++V;
        seg_in[u][0] = ++V;
        if (finish_id[u] != 0) {
            add_edge(seg_out[u][0], finish_id[u]);
        }
        if (start_id[u] != 0) {
            add_edge(start_id[u], seg_in[u][0]);
        }
    }
    for (int k = 1; k < LOG_N; k++) {
        for (int u = 1; u <= N; u++) {
            seg_out[u][k] = ++V;
            seg_in[u][k] = ++V;
            add_edge(seg_out[u][k], seg_out[u][k - 1]);
            add_edge(seg_in[u][k - 1], seg_in[u][k]);
            if (anc[u][k - 1] != 0) {
                add_edge(seg_out[u][k], seg_out[anc[u][k - 1]][k - 1]);
                add_edge(seg_in[anc[u][k - 1]][k - 1], seg_in[u][k]);
            }
        }
    }
}

int ancestor (int u, int len) {
    for (int k = 0; k < LOG_N; k++) {
        if ((len >> k) & 1) {
            u = anc[u][k];
        }
    }
    return u;
}

int lca (int u, int v) {
    if (depth[u] > depth[v]) {
        u = ancestor(u, depth[u] - depth[v]);
    }
    if (depth[v] > depth[u]) {
        v = ancestor(v, depth[v] - depth[u]);
    }
    if (u == v) {
        return u;
    }
    for (int k = LOG_N - 1; k >= 0; k--) {
        if (anc[u][k] != anc[v][k]) {
            u = anc[u][k];
            v = anc[v][k];
        }
    }
    return parent[u];
}

void add_path (int i, int u, int v) {
    int len = depth[u] - depth[v];
    for (int k = 0; k < LOG_N; k++) {
        if ((len >> k) & 1) {
            add_edge(i, seg_out[u][k]);
            add_edge(seg_in[u][k], i);
            u = anc[u][k];
        }
    }
}

void clean () {
    for (int u = 1; u <= N; u++) {
        adj[u].clear();
        parent[u] = 0;
        depth[u] = 0;
        start_id[u] = 0;
        finish_id[u] = 0;
    }
    for (int u = 1; u <= V; u++) {
        out[u].clear();
        deg[u] = 0;
    }
}

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

    cin >> Q;
    while (Q--) {
        cin >> N;
        for (int i = 1; i <= N - 1; i++) {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        dfs(1);
        cin >> M;
        for (int i = 1; i <= M; i++) {
            cin >> start[i] >> finish[i];
            start_id[start[i]] = i;
            finish_id[finish[i]] = i;
        }
        build();
        for (int i = 1; i <= M; i++) {
            int u = start[i], v = finish[i];
            int x = lca(u, v);
            if (u != x) {
                add_path(i, parent[u], x);
            }
            if (v != x) {
                add_path(i, parent[v], x);
            }
            if (finish_id[u] != 0) {
                add_edge(i, finish_id[u]);
            }
            if (start_id[v] != 0) {
                add_edge(start_id[v], i);
            }
            if (u != x && v != x) {
                if (finish_id[x] != 0) {
                    add_edge(i, finish_id[x]);
                }
                if (start_id[x] != 0) {
                    add_edge(start_id[x], i);
                }
            }
        }
        queue <int> q;
        for (int u = 1; u <= V; u++) {
            if (deg[u] == 0) {
                q.push(u);
            }
        }
        while (q.empty() == false) {
            int u = q.front();
            q.pop();
            for (int v : out[u]) {
                if (--deg[v] == 0) {
                    q.push(v);
                }
            }
        }
        bool ok = true;
        for (int u = 1; u <= V; u++) {
            if (deg[u] > 0) {
                ok = false;
                break;
            }
        }
        cout << (ok == true ? "Yes" : "No") << "\n";
        clean();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 32 ms 109916 KB Output is correct
2 Correct 25 ms 109912 KB Output is correct
3 Correct 25 ms 109916 KB Output is correct
4 Correct 49 ms 110172 KB Output is correct
5 Correct 76 ms 110172 KB Output is correct
6 Correct 27 ms 110172 KB Output is correct
7 Correct 27 ms 110172 KB Output is correct
8 Correct 28 ms 110172 KB Output is correct
9 Correct 128 ms 121188 KB Output is correct
10 Correct 386 ms 276648 KB Output is correct
11 Correct 36 ms 110240 KB Output is correct
12 Correct 89 ms 110968 KB Output is correct
13 Correct 489 ms 282708 KB Output is correct
14 Correct 381 ms 283036 KB Output is correct
15 Correct 549 ms 283412 KB Output is correct
16 Correct 849 ms 297832 KB Output is correct
17 Correct 491 ms 287448 KB Output is correct
18 Correct 449 ms 287476 KB Output is correct
19 Correct 446 ms 287572 KB Output is correct
20 Correct 420 ms 287664 KB Output is correct
21 Correct 396 ms 286600 KB Output is correct
22 Correct 372 ms 283056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 27 ms 110172 KB Output is correct
4 Correct 28 ms 110168 KB Output is correct
5 Correct 27 ms 110180 KB Output is correct
6 Correct 29 ms 110172 KB Output is correct
7 Correct 27 ms 110172 KB Output is correct
8 Correct 27 ms 110424 KB Output is correct
9 Correct 28 ms 110172 KB Output is correct
10 Correct 27 ms 110356 KB Output is correct
11 Correct 27 ms 110168 KB Output is correct
12 Correct 26 ms 110172 KB Output is correct
13 Correct 25 ms 110424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 27 ms 110172 KB Output is correct
4 Correct 28 ms 110168 KB Output is correct
5 Correct 27 ms 110180 KB Output is correct
6 Correct 29 ms 110172 KB Output is correct
7 Correct 27 ms 110172 KB Output is correct
8 Correct 27 ms 110424 KB Output is correct
9 Correct 28 ms 110172 KB Output is correct
10 Correct 27 ms 110356 KB Output is correct
11 Correct 27 ms 110168 KB Output is correct
12 Correct 26 ms 110172 KB Output is correct
13 Correct 25 ms 110424 KB Output is correct
14 Correct 24 ms 109916 KB Output is correct
15 Correct 24 ms 109916 KB Output is correct
16 Correct 29 ms 110172 KB Output is correct
17 Correct 27 ms 110168 KB Output is correct
18 Correct 27 ms 110168 KB Output is correct
19 Correct 24 ms 109916 KB Output is correct
20 Correct 28 ms 110160 KB Output is correct
21 Correct 28 ms 110172 KB Output is correct
22 Correct 45 ms 110156 KB Output is correct
23 Correct 27 ms 109912 KB Output is correct
24 Correct 25 ms 110168 KB Output is correct
25 Correct 29 ms 110376 KB Output is correct
26 Correct 28 ms 110160 KB Output is correct
27 Correct 27 ms 110168 KB Output is correct
28 Correct 24 ms 110104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 27 ms 110172 KB Output is correct
4 Correct 28 ms 110168 KB Output is correct
5 Correct 27 ms 110180 KB Output is correct
6 Correct 29 ms 110172 KB Output is correct
7 Correct 27 ms 110172 KB Output is correct
8 Correct 27 ms 110424 KB Output is correct
9 Correct 28 ms 110172 KB Output is correct
10 Correct 27 ms 110356 KB Output is correct
11 Correct 27 ms 110168 KB Output is correct
12 Correct 26 ms 110172 KB Output is correct
13 Correct 25 ms 110424 KB Output is correct
14 Correct 24 ms 109916 KB Output is correct
15 Correct 24 ms 109916 KB Output is correct
16 Correct 29 ms 110172 KB Output is correct
17 Correct 27 ms 110168 KB Output is correct
18 Correct 27 ms 110168 KB Output is correct
19 Correct 24 ms 109916 KB Output is correct
20 Correct 28 ms 110160 KB Output is correct
21 Correct 28 ms 110172 KB Output is correct
22 Correct 45 ms 110156 KB Output is correct
23 Correct 27 ms 109912 KB Output is correct
24 Correct 25 ms 110168 KB Output is correct
25 Correct 29 ms 110376 KB Output is correct
26 Correct 28 ms 110160 KB Output is correct
27 Correct 27 ms 110168 KB Output is correct
28 Correct 24 ms 110104 KB Output is correct
29 Correct 28 ms 110172 KB Output is correct
30 Correct 27 ms 110428 KB Output is correct
31 Correct 28 ms 110340 KB Output is correct
32 Correct 29 ms 110172 KB Output is correct
33 Correct 28 ms 110380 KB Output is correct
34 Correct 28 ms 110160 KB Output is correct
35 Correct 28 ms 110172 KB Output is correct
36 Correct 27 ms 110172 KB Output is correct
37 Correct 27 ms 110172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 27 ms 110172 KB Output is correct
4 Correct 28 ms 110168 KB Output is correct
5 Correct 27 ms 110180 KB Output is correct
6 Correct 29 ms 110172 KB Output is correct
7 Correct 27 ms 110172 KB Output is correct
8 Correct 27 ms 110424 KB Output is correct
9 Correct 28 ms 110172 KB Output is correct
10 Correct 27 ms 110356 KB Output is correct
11 Correct 27 ms 110168 KB Output is correct
12 Correct 26 ms 110172 KB Output is correct
13 Correct 25 ms 110424 KB Output is correct
14 Correct 24 ms 109916 KB Output is correct
15 Correct 24 ms 109916 KB Output is correct
16 Correct 29 ms 110172 KB Output is correct
17 Correct 27 ms 110168 KB Output is correct
18 Correct 27 ms 110168 KB Output is correct
19 Correct 24 ms 109916 KB Output is correct
20 Correct 28 ms 110160 KB Output is correct
21 Correct 28 ms 110172 KB Output is correct
22 Correct 45 ms 110156 KB Output is correct
23 Correct 27 ms 109912 KB Output is correct
24 Correct 25 ms 110168 KB Output is correct
25 Correct 29 ms 110376 KB Output is correct
26 Correct 28 ms 110160 KB Output is correct
27 Correct 27 ms 110168 KB Output is correct
28 Correct 24 ms 110104 KB Output is correct
29 Correct 28 ms 110172 KB Output is correct
30 Correct 27 ms 110428 KB Output is correct
31 Correct 28 ms 110340 KB Output is correct
32 Correct 29 ms 110172 KB Output is correct
33 Correct 28 ms 110380 KB Output is correct
34 Correct 28 ms 110160 KB Output is correct
35 Correct 28 ms 110172 KB Output is correct
36 Correct 27 ms 110172 KB Output is correct
37 Correct 27 ms 110172 KB Output is correct
38 Correct 118 ms 121172 KB Output is correct
39 Correct 412 ms 276804 KB Output is correct
40 Correct 140 ms 122192 KB Output is correct
41 Correct 134 ms 121168 KB Output is correct
42 Correct 112 ms 121980 KB Output is correct
43 Correct 132 ms 122256 KB Output is correct
44 Correct 39 ms 111192 KB Output is correct
45 Correct 366 ms 271396 KB Output is correct
46 Correct 367 ms 271348 KB Output is correct
47 Correct 400 ms 273232 KB Output is correct
48 Correct 400 ms 273268 KB Output is correct
49 Correct 352 ms 274004 KB Output is correct
50 Correct 376 ms 274000 KB Output is correct
51 Correct 417 ms 275032 KB Output is correct
52 Correct 421 ms 275028 KB Output is correct
53 Correct 61 ms 125724 KB Output is correct
54 Correct 485 ms 271828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 109912 KB Output is correct
2 Correct 24 ms 109916 KB Output is correct
3 Correct 24 ms 109916 KB Output is correct
4 Correct 24 ms 109912 KB Output is correct
5 Correct 35 ms 109916 KB Output is correct
6 Correct 28 ms 110680 KB Output is correct
7 Correct 26 ms 110172 KB Output is correct
8 Correct 25 ms 109916 KB Output is correct
9 Correct 24 ms 110000 KB Output is correct
10 Correct 24 ms 110168 KB Output is correct
11 Correct 25 ms 110212 KB Output is correct
12 Correct 45 ms 110172 KB Output is correct
13 Correct 59 ms 110216 KB Output is correct
14 Correct 95 ms 110732 KB Output is correct
15 Correct 71 ms 110172 KB Output is correct
16 Correct 423 ms 272208 KB Output is correct
17 Correct 452 ms 278836 KB Output is correct
18 Correct 592 ms 288800 KB Output is correct
19 Correct 441 ms 273240 KB Output is correct
20 Correct 429 ms 273432 KB Output is correct
21 Correct 413 ms 273556 KB Output is correct
22 Correct 393 ms 277888 KB Output is correct
23 Correct 387 ms 277844 KB Output is correct
24 Correct 491 ms 278252 KB Output is correct
25 Correct 489 ms 278384 KB Output is correct
26 Correct 458 ms 278104 KB Output is correct
27 Correct 400 ms 278512 KB Output is correct
28 Correct 313 ms 278460 KB Output is correct
29 Correct 314 ms 278520 KB Output is correct
30 Correct 437 ms 274124 KB Output is correct
31 Correct 355 ms 274080 KB Output is correct
32 Correct 485 ms 274128 KB Output is correct
33 Correct 331 ms 274128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 109916 KB Output is correct
2 Correct 25 ms 109912 KB Output is correct
3 Correct 25 ms 109916 KB Output is correct
4 Correct 49 ms 110172 KB Output is correct
5 Correct 76 ms 110172 KB Output is correct
6 Correct 27 ms 110172 KB Output is correct
7 Correct 27 ms 110172 KB Output is correct
8 Correct 28 ms 110172 KB Output is correct
9 Correct 128 ms 121188 KB Output is correct
10 Correct 386 ms 276648 KB Output is correct
11 Correct 36 ms 110240 KB Output is correct
12 Correct 89 ms 110968 KB Output is correct
13 Correct 489 ms 282708 KB Output is correct
14 Correct 381 ms 283036 KB Output is correct
15 Correct 549 ms 283412 KB Output is correct
16 Correct 849 ms 297832 KB Output is correct
17 Correct 491 ms 287448 KB Output is correct
18 Correct 449 ms 287476 KB Output is correct
19 Correct 446 ms 287572 KB Output is correct
20 Correct 420 ms 287664 KB Output is correct
21 Correct 396 ms 286600 KB Output is correct
22 Correct 372 ms 283056 KB Output is correct
23 Correct 24 ms 109916 KB Output is correct
24 Correct 23 ms 109916 KB Output is correct
25 Correct 27 ms 110172 KB Output is correct
26 Correct 28 ms 110168 KB Output is correct
27 Correct 27 ms 110180 KB Output is correct
28 Correct 29 ms 110172 KB Output is correct
29 Correct 27 ms 110172 KB Output is correct
30 Correct 27 ms 110424 KB Output is correct
31 Correct 28 ms 110172 KB Output is correct
32 Correct 27 ms 110356 KB Output is correct
33 Correct 27 ms 110168 KB Output is correct
34 Correct 26 ms 110172 KB Output is correct
35 Correct 25 ms 110424 KB Output is correct
36 Correct 24 ms 109916 KB Output is correct
37 Correct 24 ms 109916 KB Output is correct
38 Correct 29 ms 110172 KB Output is correct
39 Correct 27 ms 110168 KB Output is correct
40 Correct 27 ms 110168 KB Output is correct
41 Correct 24 ms 109916 KB Output is correct
42 Correct 28 ms 110160 KB Output is correct
43 Correct 28 ms 110172 KB Output is correct
44 Correct 45 ms 110156 KB Output is correct
45 Correct 27 ms 109912 KB Output is correct
46 Correct 25 ms 110168 KB Output is correct
47 Correct 29 ms 110376 KB Output is correct
48 Correct 28 ms 110160 KB Output is correct
49 Correct 27 ms 110168 KB Output is correct
50 Correct 24 ms 110104 KB Output is correct
51 Correct 28 ms 110172 KB Output is correct
52 Correct 27 ms 110428 KB Output is correct
53 Correct 28 ms 110340 KB Output is correct
54 Correct 29 ms 110172 KB Output is correct
55 Correct 28 ms 110380 KB Output is correct
56 Correct 28 ms 110160 KB Output is correct
57 Correct 28 ms 110172 KB Output is correct
58 Correct 27 ms 110172 KB Output is correct
59 Correct 27 ms 110172 KB Output is correct
60 Correct 118 ms 121172 KB Output is correct
61 Correct 412 ms 276804 KB Output is correct
62 Correct 140 ms 122192 KB Output is correct
63 Correct 134 ms 121168 KB Output is correct
64 Correct 112 ms 121980 KB Output is correct
65 Correct 132 ms 122256 KB Output is correct
66 Correct 39 ms 111192 KB Output is correct
67 Correct 366 ms 271396 KB Output is correct
68 Correct 367 ms 271348 KB Output is correct
69 Correct 400 ms 273232 KB Output is correct
70 Correct 400 ms 273268 KB Output is correct
71 Correct 352 ms 274004 KB Output is correct
72 Correct 376 ms 274000 KB Output is correct
73 Correct 417 ms 275032 KB Output is correct
74 Correct 421 ms 275028 KB Output is correct
75 Correct 61 ms 125724 KB Output is correct
76 Correct 485 ms 271828 KB Output is correct
77 Correct 24 ms 109912 KB Output is correct
78 Correct 24 ms 109916 KB Output is correct
79 Correct 24 ms 109916 KB Output is correct
80 Correct 24 ms 109912 KB Output is correct
81 Correct 35 ms 109916 KB Output is correct
82 Correct 28 ms 110680 KB Output is correct
83 Correct 26 ms 110172 KB Output is correct
84 Correct 25 ms 109916 KB Output is correct
85 Correct 24 ms 110000 KB Output is correct
86 Correct 24 ms 110168 KB Output is correct
87 Correct 25 ms 110212 KB Output is correct
88 Correct 45 ms 110172 KB Output is correct
89 Correct 59 ms 110216 KB Output is correct
90 Correct 95 ms 110732 KB Output is correct
91 Correct 71 ms 110172 KB Output is correct
92 Correct 423 ms 272208 KB Output is correct
93 Correct 452 ms 278836 KB Output is correct
94 Correct 592 ms 288800 KB Output is correct
95 Correct 441 ms 273240 KB Output is correct
96 Correct 429 ms 273432 KB Output is correct
97 Correct 413 ms 273556 KB Output is correct
98 Correct 393 ms 277888 KB Output is correct
99 Correct 387 ms 277844 KB Output is correct
100 Correct 491 ms 278252 KB Output is correct
101 Correct 489 ms 278384 KB Output is correct
102 Correct 458 ms 278104 KB Output is correct
103 Correct 400 ms 278512 KB Output is correct
104 Correct 313 ms 278460 KB Output is correct
105 Correct 314 ms 278520 KB Output is correct
106 Correct 437 ms 274124 KB Output is correct
107 Correct 355 ms 274080 KB Output is correct
108 Correct 485 ms 274128 KB Output is correct
109 Correct 331 ms 274128 KB Output is correct
110 Correct 88 ms 111204 KB Output is correct
111 Correct 74 ms 110932 KB Output is correct
112 Correct 600 ms 288436 KB Output is correct
113 Correct 551 ms 279428 KB Output is correct
114 Correct 469 ms 283952 KB Output is correct
115 Correct 253 ms 271376 KB Output is correct
116 Correct 501 ms 278316 KB Output is correct
117 Correct 706 ms 294064 KB Output is correct
118 Correct 503 ms 273224 KB Output is correct
119 Correct 484 ms 273336 KB Output is correct
120 Correct 59 ms 129624 KB Output is correct
121 Correct 666 ms 280400 KB Output is correct
122 Correct 644 ms 279888 KB Output is correct
123 Correct 597 ms 279636 KB Output is correct
124 Correct 444 ms 279840 KB Output is correct
125 Correct 589 ms 280160 KB Output is correct
126 Correct 805 ms 300576 KB Output is correct
127 Correct 520 ms 289008 KB Output is correct
128 Correct 469 ms 288856 KB Output is correct
129 Correct 546 ms 288952 KB Output is correct
130 Correct 506 ms 289012 KB Output is correct