답안 #937058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937058 2024-03-03T10:21:19 Z Pannda Jail (JOI22_jail) C++17
100 / 100
1589 ms 341212 KB
#include <bits/stdc++.h>
using namespace std;

struct Tree {
    int log, idx;
    vector<int> depth;
    vector<vector<int>> lift;
    vector<vector<int>> f;
    vector<array<int, 2>> edges;

    Tree(int n, vector<vector<int>> &adj, int root = 0) : log(32 - __builtin_clz(n)), idx(0), depth(n), lift(n, vector<int>(log, -1)), f(n, vector<int>(log, -1)) {
        auto dfs = [&](auto self, int u, int p) -> void {
            for (int v : adj[u]) {
                if (v == p) continue;
                depth[v] = depth[u] + 1;
                lift[v][0] = u;
                f[v][0] = idx++;
                for (int i = 1; i < log; i++) {
                    int w = lift[v][i - 1];
                    if (w != -1) {
                        lift[v][i] = lift[w][i - 1];
                        if (f[w][i - 1] != -1) f[v][i] = idx++;
                        if (f[w][i - 1] != -1) edges.push_back({f[v][i], f[v][i - 1]});
                        if (f[w][i - 1] != -1) edges.push_back({f[v][i], f[w][i - 1]});
                    }
                }
                self(self, v, u);
            }
        };
        depth[root] = 0;
        f[root][0] = idx++;
        dfs(dfs, root, -1);
    }

    int count() {
        return idx;
    }

    vector<array<int, 2>> getEdges() {
        return edges;
    }

    int lca(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (int i = log - 1; i >= 0; i--) {
            int w = lift[v][i];
            if (w != -1 && depth[w] >= depth[u]) v = w;
        }
        if (u == v) return u;
        for (int i = log - 1; i >= 0; i--) {
            if (lift[u][i] != lift[v][i]) {
                u = lift[u][i];
                v = lift[v][i];
            }
        }
        return lift[u][0];
    }

    int getIntermediate(int s, int t) { // not to take s == t
        int m = lca(s, t);
        if (s != m) return lift[s][0];
        for (int i = log - 1; i >= 0; i--) {
            int w = lift[t][i];
            if (w != -1 && depth[w] > depth[s]) t = w;
        }
        return t;
    }

    int getNode(int u) {
        return f[u][0];
    }

    vector<int> getPath(int u, int v) {
        vector<int> res;
        if (depth[u] > depth[v]) swap(u, v);
        for (int i = log - 1; i >= 0; i--) {
            int w = lift[v][i];
            if (w != -1 && depth[w] >= depth[u]) {
                res.push_back(f[v][i]);
                v = w;
            }
        }
        if (u == v) {
            res.push_back(f[u][0]);
            return res;
        }
        for (int i = log - 1; i >= 0; i--) {
            if (lift[u][i] != lift[v][i]) {
                res.push_back(f[u][i]);
                res.push_back(f[v][i]);
                u = lift[u][i];
                v = lift[v][i];
            }
        }
        res.push_back(f[u][1]);
        res.push_back(f[v][0]);
        return res;
    }
};

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

    int t;
    cin >> t;
    while (t--) {

        int n;
        cin >> n;
        vector<vector<int>> adj(n);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        int m;
        cin >> m;

        Tree tree(n, adj);
        vector<vector<int>> dag(2 * tree.count() + m);
        for (auto [u, v] : tree.getEdges()) {
            dag[v].push_back(u);
            dag[tree.count() + u].push_back(tree.count() + v);
        }

        for (int i = 0; i < m; i++) {
            int s, t;
            cin >> s >> t;
            s--;
            t--;
            int u = 2 * tree.count() + i;
            dag[u].push_back(tree.getNode(s));
            dag[u].push_back(tree.count() + tree.getNode(s));
            dag[tree.getNode(t)].push_back(u);
            dag[tree.count() + tree.getNode(t)].push_back(u);
            s = tree.getIntermediate(s, t);
            if (s == t) continue;
            t = tree.getIntermediate(t, s);
            vector<int> path = tree.getPath(s, t);
            for (int v : path) {
                dag[v].push_back(u);
                dag[u].push_back(tree.count() + v);
            }
        }

        vector<int> deg(2 * tree.count() + m, 0);
        for (int u = 0; u < 2 * tree.count() + m; u++) {
            for (int v : dag[u]) {
                deg[v]++;
            }
        }

        vector<int> que;
        for (int u = 0; u < 2 * tree.count() + m; u++) {
            if (!deg[u]) {
                que.push_back(u);
            }
        }

        for (int i = 0; i < que.size(); i++) {
            int u = que[i];
            for (int v : dag[u]) {
                if (!--deg[v]) {
                    que.push_back(v);
                }
            }
        }

        que.size() == 2 * tree.count() + m ? cout << "Yes\n" : cout << "No\n";

    }
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:165:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |         for (int i = 0; i < que.size(); i++) {
      |                         ~~^~~~~~~~~~~~
jail.cpp:174:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  174 |         que.size() == 2 * tree.count() + m ? cout << "Yes\n" : cout << "No\n";
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 61 ms 940 KB Output is correct
5 Correct 142 ms 1212 KB Output is correct
6 Correct 7 ms 600 KB Output is correct
7 Correct 7 ms 608 KB Output is correct
8 Correct 8 ms 860 KB Output is correct
9 Correct 372 ms 14640 KB Output is correct
10 Correct 931 ms 330028 KB Output is correct
11 Correct 20 ms 604 KB Output is correct
12 Correct 165 ms 1584 KB Output is correct
13 Correct 1054 ms 334840 KB Output is correct
14 Correct 903 ms 318404 KB Output is correct
15 Correct 1152 ms 312788 KB Output is correct
16 Correct 1589 ms 318100 KB Output is correct
17 Correct 1238 ms 338608 KB Output is correct
18 Correct 1048 ms 341212 KB Output is correct
19 Correct 1132 ms 337632 KB Output is correct
20 Correct 1095 ms 334648 KB Output is correct
21 Correct 869 ms 313484 KB Output is correct
22 Correct 866 ms 318948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 732 KB Output is correct
5 Correct 5 ms 732 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 5 ms 600 KB Output is correct
8 Correct 5 ms 600 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 732 KB Output is correct
5 Correct 5 ms 732 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 5 ms 600 KB Output is correct
8 Correct 5 ms 600 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 7 ms 604 KB Output is correct
17 Correct 4 ms 664 KB Output is correct
18 Correct 10 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 7 ms 604 KB Output is correct
21 Correct 7 ms 792 KB Output is correct
22 Correct 4 ms 604 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 6 ms 600 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 4 ms 600 KB Output is correct
28 Correct 1 ms 612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 732 KB Output is correct
5 Correct 5 ms 732 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 5 ms 600 KB Output is correct
8 Correct 5 ms 600 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 7 ms 604 KB Output is correct
17 Correct 4 ms 664 KB Output is correct
18 Correct 10 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 7 ms 604 KB Output is correct
21 Correct 7 ms 792 KB Output is correct
22 Correct 4 ms 604 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 6 ms 600 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 4 ms 600 KB Output is correct
28 Correct 1 ms 612 KB Output is correct
29 Correct 9 ms 856 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 8 ms 724 KB Output is correct
32 Correct 9 ms 604 KB Output is correct
33 Correct 7 ms 600 KB Output is correct
34 Correct 5 ms 604 KB Output is correct
35 Correct 7 ms 604 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 732 KB Output is correct
5 Correct 5 ms 732 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 5 ms 600 KB Output is correct
8 Correct 5 ms 600 KB Output is correct
9 Correct 5 ms 724 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 7 ms 604 KB Output is correct
17 Correct 4 ms 664 KB Output is correct
18 Correct 10 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 7 ms 604 KB Output is correct
21 Correct 7 ms 792 KB Output is correct
22 Correct 4 ms 604 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 6 ms 600 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 4 ms 600 KB Output is correct
28 Correct 1 ms 612 KB Output is correct
29 Correct 9 ms 856 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 8 ms 724 KB Output is correct
32 Correct 9 ms 604 KB Output is correct
33 Correct 7 ms 600 KB Output is correct
34 Correct 5 ms 604 KB Output is correct
35 Correct 7 ms 604 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 375 ms 14460 KB Output is correct
39 Correct 901 ms 330368 KB Output is correct
40 Correct 248 ms 15240 KB Output is correct
41 Correct 136 ms 6560 KB Output is correct
42 Correct 116 ms 8224 KB Output is correct
43 Correct 390 ms 12680 KB Output is correct
44 Correct 32 ms 1288 KB Output is correct
45 Correct 318 ms 142216 KB Output is correct
46 Correct 353 ms 145504 KB Output is correct
47 Correct 861 ms 302020 KB Output is correct
48 Correct 852 ms 303720 KB Output is correct
49 Correct 568 ms 222672 KB Output is correct
50 Correct 522 ms 225096 KB Output is correct
51 Correct 654 ms 276972 KB Output is correct
52 Correct 690 ms 274120 KB Output is correct
53 Correct 34 ms 8300 KB Output is correct
54 Correct 264 ms 126060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 452 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 1 ms 456 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 20 ms 620 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 5 ms 860 KB Output is correct
13 Correct 62 ms 856 KB Output is correct
14 Correct 107 ms 1360 KB Output is correct
15 Correct 88 ms 1164 KB Output is correct
16 Correct 263 ms 97628 KB Output is correct
17 Correct 416 ms 102784 KB Output is correct
18 Correct 600 ms 111676 KB Output is correct
19 Correct 293 ms 99700 KB Output is correct
20 Correct 306 ms 99000 KB Output is correct
21 Correct 322 ms 98464 KB Output is correct
22 Correct 406 ms 106276 KB Output is correct
23 Correct 330 ms 105616 KB Output is correct
24 Correct 384 ms 107268 KB Output is correct
25 Correct 363 ms 108288 KB Output is correct
26 Correct 334 ms 107412 KB Output is correct
27 Correct 318 ms 75808 KB Output is correct
28 Correct 272 ms 70468 KB Output is correct
29 Correct 289 ms 70380 KB Output is correct
30 Correct 293 ms 78440 KB Output is correct
31 Correct 256 ms 74040 KB Output is correct
32 Correct 268 ms 79172 KB Output is correct
33 Correct 244 ms 74072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 61 ms 940 KB Output is correct
5 Correct 142 ms 1212 KB Output is correct
6 Correct 7 ms 600 KB Output is correct
7 Correct 7 ms 608 KB Output is correct
8 Correct 8 ms 860 KB Output is correct
9 Correct 372 ms 14640 KB Output is correct
10 Correct 931 ms 330028 KB Output is correct
11 Correct 20 ms 604 KB Output is correct
12 Correct 165 ms 1584 KB Output is correct
13 Correct 1054 ms 334840 KB Output is correct
14 Correct 903 ms 318404 KB Output is correct
15 Correct 1152 ms 312788 KB Output is correct
16 Correct 1589 ms 318100 KB Output is correct
17 Correct 1238 ms 338608 KB Output is correct
18 Correct 1048 ms 341212 KB Output is correct
19 Correct 1132 ms 337632 KB Output is correct
20 Correct 1095 ms 334648 KB Output is correct
21 Correct 869 ms 313484 KB Output is correct
22 Correct 866 ms 318948 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 7 ms 604 KB Output is correct
26 Correct 7 ms 732 KB Output is correct
27 Correct 5 ms 732 KB Output is correct
28 Correct 5 ms 604 KB Output is correct
29 Correct 5 ms 600 KB Output is correct
30 Correct 5 ms 600 KB Output is correct
31 Correct 5 ms 724 KB Output is correct
32 Correct 8 ms 604 KB Output is correct
33 Correct 5 ms 724 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 7 ms 604 KB Output is correct
39 Correct 4 ms 664 KB Output is correct
40 Correct 10 ms 604 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 7 ms 604 KB Output is correct
43 Correct 7 ms 792 KB Output is correct
44 Correct 4 ms 604 KB Output is correct
45 Correct 1 ms 600 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 6 ms 600 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 4 ms 600 KB Output is correct
50 Correct 1 ms 612 KB Output is correct
51 Correct 9 ms 856 KB Output is correct
52 Correct 5 ms 604 KB Output is correct
53 Correct 8 ms 724 KB Output is correct
54 Correct 9 ms 604 KB Output is correct
55 Correct 7 ms 600 KB Output is correct
56 Correct 5 ms 604 KB Output is correct
57 Correct 7 ms 604 KB Output is correct
58 Correct 3 ms 600 KB Output is correct
59 Correct 3 ms 604 KB Output is correct
60 Correct 375 ms 14460 KB Output is correct
61 Correct 901 ms 330368 KB Output is correct
62 Correct 248 ms 15240 KB Output is correct
63 Correct 136 ms 6560 KB Output is correct
64 Correct 116 ms 8224 KB Output is correct
65 Correct 390 ms 12680 KB Output is correct
66 Correct 32 ms 1288 KB Output is correct
67 Correct 318 ms 142216 KB Output is correct
68 Correct 353 ms 145504 KB Output is correct
69 Correct 861 ms 302020 KB Output is correct
70 Correct 852 ms 303720 KB Output is correct
71 Correct 568 ms 222672 KB Output is correct
72 Correct 522 ms 225096 KB Output is correct
73 Correct 654 ms 276972 KB Output is correct
74 Correct 690 ms 274120 KB Output is correct
75 Correct 34 ms 8300 KB Output is correct
76 Correct 264 ms 126060 KB Output is correct
77 Correct 1 ms 452 KB Output is correct
78 Correct 0 ms 460 KB Output is correct
79 Correct 1 ms 456 KB Output is correct
80 Correct 0 ms 344 KB Output is correct
81 Correct 20 ms 620 KB Output is correct
82 Correct 1 ms 348 KB Output is correct
83 Correct 2 ms 604 KB Output is correct
84 Correct 1 ms 344 KB Output is correct
85 Correct 1 ms 348 KB Output is correct
86 Correct 1 ms 348 KB Output is correct
87 Correct 1 ms 600 KB Output is correct
88 Correct 5 ms 860 KB Output is correct
89 Correct 62 ms 856 KB Output is correct
90 Correct 107 ms 1360 KB Output is correct
91 Correct 88 ms 1164 KB Output is correct
92 Correct 263 ms 97628 KB Output is correct
93 Correct 416 ms 102784 KB Output is correct
94 Correct 600 ms 111676 KB Output is correct
95 Correct 293 ms 99700 KB Output is correct
96 Correct 306 ms 99000 KB Output is correct
97 Correct 322 ms 98464 KB Output is correct
98 Correct 406 ms 106276 KB Output is correct
99 Correct 330 ms 105616 KB Output is correct
100 Correct 384 ms 107268 KB Output is correct
101 Correct 363 ms 108288 KB Output is correct
102 Correct 334 ms 107412 KB Output is correct
103 Correct 318 ms 75808 KB Output is correct
104 Correct 272 ms 70468 KB Output is correct
105 Correct 289 ms 70380 KB Output is correct
106 Correct 293 ms 78440 KB Output is correct
107 Correct 256 ms 74040 KB Output is correct
108 Correct 268 ms 79172 KB Output is correct
109 Correct 244 ms 74072 KB Output is correct
110 Correct 105 ms 1524 KB Output is correct
111 Correct 93 ms 1316 KB Output is correct
112 Correct 977 ms 294216 KB Output is correct
113 Correct 969 ms 308372 KB Output is correct
114 Correct 528 ms 181904 KB Output is correct
115 Correct 83 ms 60652 KB Output is correct
116 Correct 378 ms 125144 KB Output is correct
117 Correct 669 ms 130176 KB Output is correct
118 Correct 248 ms 118400 KB Output is correct
119 Correct 254 ms 120180 KB Output is correct
120 Correct 22 ms 11032 KB Output is correct
121 Correct 528 ms 150004 KB Output is correct
122 Correct 506 ms 142944 KB Output is correct
123 Correct 1061 ms 317592 KB Output is correct
124 Correct 807 ms 302360 KB Output is correct
125 Correct 992 ms 318856 KB Output is correct
126 Correct 1281 ms 314144 KB Output is correct
127 Correct 1090 ms 293720 KB Output is correct
128 Correct 853 ms 294000 KB Output is correct
129 Correct 1078 ms 312300 KB Output is correct
130 Correct 924 ms 294868 KB Output is correct