Submission #781669

# Submission time Handle Problem Language Result Execution time Memory
781669 2023-07-13T09:33:29 Z IBory Jail (JOI22_jail) C++17
100 / 100
951 ms 100292 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;

const int SZ = 1 << 17;
vector<int> TG[SZ], G[SZ], G2[SZ << 3];
pii E[SZ];
int D[SZ], P[17][SZ], Z[SZ], R[SZ], in[SZ], out[SZ], id[SZ << 3], dn;

void MakeG(int cur, int prev) {
    P[0][cur] = prev;
    D[cur] = D[prev] + 1;
    for (int next : TG[cur]) {
        if (next == prev) continue;
        G[cur].push_back(next);
        MakeG(next, cur);
    }
}

int DFS(int cur) {
    int& ret = Z[cur] = 1;
    for (int i = 0; i < G[cur].size(); ++i) {
        int next = G[cur][i];
        ret += DFS(next);
        if (Z[next] > Z[G[cur][0]]) swap(G[cur][0], G[cur][i]);
    }
    return ret;
}

void DFS2(int cur, int top) {
    in[cur] = ++dn;
    R[cur] = top;
    for (int i = 0; i < G[cur].size(); ++i) {
        int next = G[cur][i];
        DFS2(next, (i == 0 ? top : next));
    }
    out[cur] = dn;
}

int LCA(int a, int b) {
    while (R[a] != R[b]) {
        if (D[R[a]] > D[R[b]]) swap(a, b);
        b = P[0][R[b]];
    }
    return (D[a] > D[b] ? b : a);
}

void Segment(int L, int R, vector<int>& node, int sL, int sR, int n = 1) {
    if (R < sL || sR < L) return;
    if (L <= sL && sR <= R) {
        node.push_back(n);
        return;
    }
    int mid = (sL + sR) >> 1;
    Segment(L, R, node, sL, mid, n * 2);
    Segment(L, R, node, mid + 1, sR, n * 2 + 1);
}

vector<int> Decompose(int s, int e, int CZ) {
    vector<pii> seg;
    vector<int> ret;
    int c = LCA(s, e);
    if (c == s) {
        int t = e, d = D[e] - D[s] - 1;
        for (int i = 16; i >= 0; --i) if (d & (1 << i)) t = P[i][t];
        s = t;
    }
    else s = P[0][s];

    while (R[s] != R[e]) {
        if (D[R[s]] > D[R[e]]) swap(s, e);
        seg.emplace_back(in[R[e]], in[e]);
        e = P[0][R[e]];
    }
    if (D[s] > D[e]) swap(s, e);
    seg.emplace_back(in[s], in[e]);

    for (auto [a, b] : seg) Segment(a, b, ret, 1, CZ);
    return ret;
}

int main() {
    int test;
    cin >> test;
    while (test--) {
        int N, M, no = 0, CZ = 1;
        cin >> N;
        for (int i = 1; i < N; ++i) {
            int a, b;
            cin >> a >> b;
            TG[a].push_back(b);
            TG[b].push_back(a);
        }
        cin >> M;
        for (int i = 1; i <= M; ++i) cin >> E[i].first >> E[i].second;
        while (CZ < N) CZ <<= 1;

        dn = 0;
        MakeG(1, 1);
        DFS(1);
        DFS2(1, 1);
        for (int i = 1; i < 17; ++i) for (int n = 1; n <= N; ++n) P[i][n] = P[i - 1][P[i - 1][n]];

        memset(id, 0, sizeof(id));
        for (int i = 1; i <= M; ++i) {
            auto [s, e] = E[i];
            vector<int> from = Decompose(s, e, CZ);
            vector<int> to = Decompose(e, s, CZ);
            for (int& n : to) n += CZ * 2;
            int cur = CZ * 4 + i;
            for (int n : from) G2[n].push_back(cur), id[cur]++;
            for (int n : to) G2[cur].push_back(n), id[n]++;
            G2[cur].push_back(in[s] + CZ - 1), id[in[s] + CZ - 1]++;
            G2[in[e] + CZ * 3 - 1].push_back(cur), id[cur]++;
        }
        
        for (int i = 1; i < CZ; ++i) {
            G2[i * 2].push_back(i), id[i]++;
            G2[i * 2 + 1].push_back(i), id[i]++;
            int t = CZ * 2;
            G2[t + i].push_back(t + i * 2), id[t + i * 2]++;
            G2[t + i].push_back(t + i * 2 + 1), id[t + i * 2 + 1]++;
        }

        queue<int> Q;
        int cnt = CZ * 4 + M;
        for (int i = 1; i <= CZ * 4 + M; ++i) {
            if (!id[i]) Q.push(i);
        }

        while (!Q.empty()) {
            int cur = Q.front(); Q.pop();
            cnt--;
            for (int next : G2[cur]) {
                if (!--id[next]) Q.push(next);
            }
        }

        cout << (cnt ? "No" : "Yes") << '\n';
        for (int i = 1; i <= N; ++i) {
            G[i].clear();
            TG[i].clear();
        }
        for (int i = 0; i < (CZ * 8); ++i) G2[i].clear();
    }
    return 0;
}

Compilation message

jail.cpp: In function 'int DFS(int)':
jail.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for (int i = 0; i < G[cur].size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~~
jail.cpp: In function 'void DFS2(int, int)':
jail.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i = 0; i < G[cur].size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~~
jail.cpp: In function 'int main()':
jail.cpp:86:19: warning: unused variable 'no' [-Wunused-variable]
   86 |         int N, M, no = 0, CZ = 1;
      |                   ^~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 35284 KB Output is correct
2 Correct 18 ms 35336 KB Output is correct
3 Correct 16 ms 35304 KB Output is correct
4 Correct 82 ms 35556 KB Output is correct
5 Correct 127 ms 35936 KB Output is correct
6 Correct 19 ms 35284 KB Output is correct
7 Correct 20 ms 35284 KB Output is correct
8 Correct 21 ms 35412 KB Output is correct
9 Correct 98 ms 38816 KB Output is correct
10 Correct 112 ms 74952 KB Output is correct
11 Correct 98 ms 35412 KB Output is correct
12 Correct 188 ms 36192 KB Output is correct
13 Correct 241 ms 80604 KB Output is correct
14 Correct 237 ms 80716 KB Output is correct
15 Correct 315 ms 85256 KB Output is correct
16 Correct 590 ms 100292 KB Output is correct
17 Correct 329 ms 90068 KB Output is correct
18 Correct 241 ms 84408 KB Output is correct
19 Correct 253 ms 88460 KB Output is correct
20 Correct 263 ms 88556 KB Output is correct
21 Correct 271 ms 88944 KB Output is correct
22 Correct 159 ms 79516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 35284 KB Output is correct
2 Correct 17 ms 35224 KB Output is correct
3 Correct 23 ms 35328 KB Output is correct
4 Correct 22 ms 35284 KB Output is correct
5 Correct 22 ms 35284 KB Output is correct
6 Correct 22 ms 35316 KB Output is correct
7 Correct 22 ms 35288 KB Output is correct
8 Correct 20 ms 35324 KB Output is correct
9 Correct 21 ms 35344 KB Output is correct
10 Correct 22 ms 35320 KB Output is correct
11 Correct 20 ms 35276 KB Output is correct
12 Correct 20 ms 35284 KB Output is correct
13 Correct 20 ms 35248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 35284 KB Output is correct
2 Correct 17 ms 35224 KB Output is correct
3 Correct 23 ms 35328 KB Output is correct
4 Correct 22 ms 35284 KB Output is correct
5 Correct 22 ms 35284 KB Output is correct
6 Correct 22 ms 35316 KB Output is correct
7 Correct 22 ms 35288 KB Output is correct
8 Correct 20 ms 35324 KB Output is correct
9 Correct 21 ms 35344 KB Output is correct
10 Correct 22 ms 35320 KB Output is correct
11 Correct 20 ms 35276 KB Output is correct
12 Correct 20 ms 35284 KB Output is correct
13 Correct 20 ms 35248 KB Output is correct
14 Correct 17 ms 35284 KB Output is correct
15 Correct 17 ms 35284 KB Output is correct
16 Correct 21 ms 35284 KB Output is correct
17 Correct 23 ms 35376 KB Output is correct
18 Correct 22 ms 35284 KB Output is correct
19 Correct 19 ms 35284 KB Output is correct
20 Correct 25 ms 35380 KB Output is correct
21 Correct 21 ms 35368 KB Output is correct
22 Correct 22 ms 35284 KB Output is correct
23 Correct 19 ms 35244 KB Output is correct
24 Correct 19 ms 35308 KB Output is correct
25 Correct 22 ms 35316 KB Output is correct
26 Correct 20 ms 35316 KB Output is correct
27 Correct 25 ms 35296 KB Output is correct
28 Correct 22 ms 35284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 35284 KB Output is correct
2 Correct 17 ms 35224 KB Output is correct
3 Correct 23 ms 35328 KB Output is correct
4 Correct 22 ms 35284 KB Output is correct
5 Correct 22 ms 35284 KB Output is correct
6 Correct 22 ms 35316 KB Output is correct
7 Correct 22 ms 35288 KB Output is correct
8 Correct 20 ms 35324 KB Output is correct
9 Correct 21 ms 35344 KB Output is correct
10 Correct 22 ms 35320 KB Output is correct
11 Correct 20 ms 35276 KB Output is correct
12 Correct 20 ms 35284 KB Output is correct
13 Correct 20 ms 35248 KB Output is correct
14 Correct 17 ms 35284 KB Output is correct
15 Correct 17 ms 35284 KB Output is correct
16 Correct 21 ms 35284 KB Output is correct
17 Correct 23 ms 35376 KB Output is correct
18 Correct 22 ms 35284 KB Output is correct
19 Correct 19 ms 35284 KB Output is correct
20 Correct 25 ms 35380 KB Output is correct
21 Correct 21 ms 35368 KB Output is correct
22 Correct 22 ms 35284 KB Output is correct
23 Correct 19 ms 35244 KB Output is correct
24 Correct 19 ms 35308 KB Output is correct
25 Correct 22 ms 35316 KB Output is correct
26 Correct 20 ms 35316 KB Output is correct
27 Correct 25 ms 35296 KB Output is correct
28 Correct 22 ms 35284 KB Output is correct
29 Correct 22 ms 35408 KB Output is correct
30 Correct 22 ms 35320 KB Output is correct
31 Correct 22 ms 35316 KB Output is correct
32 Correct 23 ms 35284 KB Output is correct
33 Correct 22 ms 35328 KB Output is correct
34 Correct 23 ms 35324 KB Output is correct
35 Correct 23 ms 35316 KB Output is correct
36 Correct 21 ms 35392 KB Output is correct
37 Correct 20 ms 35320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 35284 KB Output is correct
2 Correct 17 ms 35224 KB Output is correct
3 Correct 23 ms 35328 KB Output is correct
4 Correct 22 ms 35284 KB Output is correct
5 Correct 22 ms 35284 KB Output is correct
6 Correct 22 ms 35316 KB Output is correct
7 Correct 22 ms 35288 KB Output is correct
8 Correct 20 ms 35324 KB Output is correct
9 Correct 21 ms 35344 KB Output is correct
10 Correct 22 ms 35320 KB Output is correct
11 Correct 20 ms 35276 KB Output is correct
12 Correct 20 ms 35284 KB Output is correct
13 Correct 20 ms 35248 KB Output is correct
14 Correct 17 ms 35284 KB Output is correct
15 Correct 17 ms 35284 KB Output is correct
16 Correct 21 ms 35284 KB Output is correct
17 Correct 23 ms 35376 KB Output is correct
18 Correct 22 ms 35284 KB Output is correct
19 Correct 19 ms 35284 KB Output is correct
20 Correct 25 ms 35380 KB Output is correct
21 Correct 21 ms 35368 KB Output is correct
22 Correct 22 ms 35284 KB Output is correct
23 Correct 19 ms 35244 KB Output is correct
24 Correct 19 ms 35308 KB Output is correct
25 Correct 22 ms 35316 KB Output is correct
26 Correct 20 ms 35316 KB Output is correct
27 Correct 25 ms 35296 KB Output is correct
28 Correct 22 ms 35284 KB Output is correct
29 Correct 22 ms 35408 KB Output is correct
30 Correct 22 ms 35320 KB Output is correct
31 Correct 22 ms 35316 KB Output is correct
32 Correct 23 ms 35284 KB Output is correct
33 Correct 22 ms 35328 KB Output is correct
34 Correct 23 ms 35324 KB Output is correct
35 Correct 23 ms 35316 KB Output is correct
36 Correct 21 ms 35392 KB Output is correct
37 Correct 20 ms 35320 KB Output is correct
38 Correct 93 ms 38820 KB Output is correct
39 Correct 143 ms 74952 KB Output is correct
40 Correct 98 ms 38736 KB Output is correct
41 Correct 109 ms 38644 KB Output is correct
42 Correct 86 ms 38860 KB Output is correct
43 Correct 88 ms 38316 KB Output is correct
44 Correct 60 ms 35912 KB Output is correct
45 Correct 127 ms 66160 KB Output is correct
46 Correct 129 ms 66040 KB Output is correct
47 Correct 135 ms 70096 KB Output is correct
48 Correct 121 ms 70120 KB Output is correct
49 Correct 126 ms 66332 KB Output is correct
50 Correct 123 ms 66252 KB Output is correct
51 Correct 133 ms 67320 KB Output is correct
52 Correct 122 ms 67292 KB Output is correct
53 Correct 43 ms 37616 KB Output is correct
54 Correct 132 ms 65740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 35184 KB Output is correct
2 Correct 18 ms 35244 KB Output is correct
3 Correct 17 ms 35284 KB Output is correct
4 Correct 17 ms 35284 KB Output is correct
5 Correct 110 ms 35368 KB Output is correct
6 Correct 19 ms 35284 KB Output is correct
7 Correct 20 ms 35332 KB Output is correct
8 Correct 19 ms 35284 KB Output is correct
9 Correct 19 ms 35220 KB Output is correct
10 Correct 19 ms 35284 KB Output is correct
11 Correct 18 ms 35208 KB Output is correct
12 Correct 21 ms 35320 KB Output is correct
13 Correct 140 ms 35860 KB Output is correct
14 Correct 171 ms 36172 KB Output is correct
15 Correct 151 ms 36044 KB Output is correct
16 Correct 188 ms 67052 KB Output is correct
17 Correct 425 ms 75832 KB Output is correct
18 Correct 727 ms 86944 KB Output is correct
19 Correct 249 ms 68760 KB Output is correct
20 Correct 225 ms 68828 KB Output is correct
21 Correct 242 ms 68832 KB Output is correct
22 Correct 400 ms 75956 KB Output is correct
23 Correct 348 ms 75868 KB Output is correct
24 Correct 377 ms 75900 KB Output is correct
25 Correct 428 ms 75768 KB Output is correct
26 Correct 371 ms 76008 KB Output is correct
27 Correct 339 ms 74652 KB Output is correct
28 Correct 297 ms 74612 KB Output is correct
29 Correct 324 ms 74592 KB Output is correct
30 Correct 291 ms 70984 KB Output is correct
31 Correct 230 ms 70884 KB Output is correct
32 Correct 287 ms 70896 KB Output is correct
33 Correct 228 ms 70856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 35284 KB Output is correct
2 Correct 18 ms 35336 KB Output is correct
3 Correct 16 ms 35304 KB Output is correct
4 Correct 82 ms 35556 KB Output is correct
5 Correct 127 ms 35936 KB Output is correct
6 Correct 19 ms 35284 KB Output is correct
7 Correct 20 ms 35284 KB Output is correct
8 Correct 21 ms 35412 KB Output is correct
9 Correct 98 ms 38816 KB Output is correct
10 Correct 112 ms 74952 KB Output is correct
11 Correct 98 ms 35412 KB Output is correct
12 Correct 188 ms 36192 KB Output is correct
13 Correct 241 ms 80604 KB Output is correct
14 Correct 237 ms 80716 KB Output is correct
15 Correct 315 ms 85256 KB Output is correct
16 Correct 590 ms 100292 KB Output is correct
17 Correct 329 ms 90068 KB Output is correct
18 Correct 241 ms 84408 KB Output is correct
19 Correct 253 ms 88460 KB Output is correct
20 Correct 263 ms 88556 KB Output is correct
21 Correct 271 ms 88944 KB Output is correct
22 Correct 159 ms 79516 KB Output is correct
23 Correct 17 ms 35284 KB Output is correct
24 Correct 17 ms 35224 KB Output is correct
25 Correct 23 ms 35328 KB Output is correct
26 Correct 22 ms 35284 KB Output is correct
27 Correct 22 ms 35284 KB Output is correct
28 Correct 22 ms 35316 KB Output is correct
29 Correct 22 ms 35288 KB Output is correct
30 Correct 20 ms 35324 KB Output is correct
31 Correct 21 ms 35344 KB Output is correct
32 Correct 22 ms 35320 KB Output is correct
33 Correct 20 ms 35276 KB Output is correct
34 Correct 20 ms 35284 KB Output is correct
35 Correct 20 ms 35248 KB Output is correct
36 Correct 17 ms 35284 KB Output is correct
37 Correct 17 ms 35284 KB Output is correct
38 Correct 21 ms 35284 KB Output is correct
39 Correct 23 ms 35376 KB Output is correct
40 Correct 22 ms 35284 KB Output is correct
41 Correct 19 ms 35284 KB Output is correct
42 Correct 25 ms 35380 KB Output is correct
43 Correct 21 ms 35368 KB Output is correct
44 Correct 22 ms 35284 KB Output is correct
45 Correct 19 ms 35244 KB Output is correct
46 Correct 19 ms 35308 KB Output is correct
47 Correct 22 ms 35316 KB Output is correct
48 Correct 20 ms 35316 KB Output is correct
49 Correct 25 ms 35296 KB Output is correct
50 Correct 22 ms 35284 KB Output is correct
51 Correct 22 ms 35408 KB Output is correct
52 Correct 22 ms 35320 KB Output is correct
53 Correct 22 ms 35316 KB Output is correct
54 Correct 23 ms 35284 KB Output is correct
55 Correct 22 ms 35328 KB Output is correct
56 Correct 23 ms 35324 KB Output is correct
57 Correct 23 ms 35316 KB Output is correct
58 Correct 21 ms 35392 KB Output is correct
59 Correct 20 ms 35320 KB Output is correct
60 Correct 93 ms 38820 KB Output is correct
61 Correct 143 ms 74952 KB Output is correct
62 Correct 98 ms 38736 KB Output is correct
63 Correct 109 ms 38644 KB Output is correct
64 Correct 86 ms 38860 KB Output is correct
65 Correct 88 ms 38316 KB Output is correct
66 Correct 60 ms 35912 KB Output is correct
67 Correct 127 ms 66160 KB Output is correct
68 Correct 129 ms 66040 KB Output is correct
69 Correct 135 ms 70096 KB Output is correct
70 Correct 121 ms 70120 KB Output is correct
71 Correct 126 ms 66332 KB Output is correct
72 Correct 123 ms 66252 KB Output is correct
73 Correct 133 ms 67320 KB Output is correct
74 Correct 122 ms 67292 KB Output is correct
75 Correct 43 ms 37616 KB Output is correct
76 Correct 132 ms 65740 KB Output is correct
77 Correct 17 ms 35184 KB Output is correct
78 Correct 18 ms 35244 KB Output is correct
79 Correct 17 ms 35284 KB Output is correct
80 Correct 17 ms 35284 KB Output is correct
81 Correct 110 ms 35368 KB Output is correct
82 Correct 19 ms 35284 KB Output is correct
83 Correct 20 ms 35332 KB Output is correct
84 Correct 19 ms 35284 KB Output is correct
85 Correct 19 ms 35220 KB Output is correct
86 Correct 19 ms 35284 KB Output is correct
87 Correct 18 ms 35208 KB Output is correct
88 Correct 21 ms 35320 KB Output is correct
89 Correct 140 ms 35860 KB Output is correct
90 Correct 171 ms 36172 KB Output is correct
91 Correct 151 ms 36044 KB Output is correct
92 Correct 188 ms 67052 KB Output is correct
93 Correct 425 ms 75832 KB Output is correct
94 Correct 727 ms 86944 KB Output is correct
95 Correct 249 ms 68760 KB Output is correct
96 Correct 225 ms 68828 KB Output is correct
97 Correct 242 ms 68832 KB Output is correct
98 Correct 400 ms 75956 KB Output is correct
99 Correct 348 ms 75868 KB Output is correct
100 Correct 377 ms 75900 KB Output is correct
101 Correct 428 ms 75768 KB Output is correct
102 Correct 371 ms 76008 KB Output is correct
103 Correct 339 ms 74652 KB Output is correct
104 Correct 297 ms 74612 KB Output is correct
105 Correct 324 ms 74592 KB Output is correct
106 Correct 291 ms 70984 KB Output is correct
107 Correct 230 ms 70884 KB Output is correct
108 Correct 287 ms 70896 KB Output is correct
109 Correct 228 ms 70856 KB Output is correct
110 Correct 168 ms 36300 KB Output is correct
111 Correct 119 ms 35940 KB Output is correct
112 Correct 361 ms 82556 KB Output is correct
113 Correct 200 ms 74188 KB Output is correct
114 Correct 256 ms 78064 KB Output is correct
115 Correct 103 ms 64520 KB Output is correct
116 Correct 249 ms 69872 KB Output is correct
117 Correct 951 ms 97496 KB Output is correct
118 Correct 146 ms 65808 KB Output is correct
119 Correct 166 ms 65980 KB Output is correct
120 Correct 37 ms 38776 KB Output is correct
121 Correct 328 ms 71852 KB Output is correct
122 Correct 344 ms 71792 KB Output is correct
123 Correct 199 ms 76552 KB Output is correct
124 Correct 194 ms 76440 KB Output is correct
125 Correct 242 ms 77212 KB Output is correct
126 Correct 578 ms 97184 KB Output is correct
127 Correct 305 ms 85568 KB Output is correct
128 Correct 303 ms 85800 KB Output is correct
129 Correct 303 ms 85684 KB Output is correct
130 Correct 286 ms 85836 KB Output is correct