Submission #867165

# Submission time Handle Problem Language Result Execution time Memory
867165 2023-10-27T19:24:17 Z nima_aryan Jail (JOI22_jail) C++17
100 / 100
2227 ms 562220 KB
/**
 *    author:  NimaAryan
 *    created: 2023-10-27 20:13:43
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int Q;
  cin >> Q;
  while (Q--) {
    int N;
    cin >> N;
    vector<vector<int>> t(N);
    for (int i = 0; i < N - 1; ++i) {
      int A, B;
      cin >> A >> B;
      --A, --B;
      t[A].push_back(B);
      t[B].push_back(A);
    }
    int M;
    cin >> M;
    vector<int> S(M), T(M);
    vector<int> whoS(N, -1), whoT(N, -1);
    for (int i = 0; i < M; ++i) {
      cin >> S[i] >> T[i];
      --S[i], --T[i];
      whoS[S[i]] = i;
      whoT[T[i]] = i;
    }

    vector up(32, vector<int>(N, -1));
    vector<int> par(N), dep(N);

    /* i <- S(j), i -> T(j) */
    vector<vector<int>> d(M);
    int m = M;
    auto add_edge = [&](int a, int b) {
      if (a == m || b == m) {
        d.emplace_back();
      }
      if (a == -1 || b == -1 || a == b) {
        return;
      }
      d[a].push_back(b);
    };
    vector fakeS(32, vector<int>(N, -1));
    vector fakeT(32, vector<int>(N, -1));
    auto dfs = [&](auto self, int v, int p) -> void {
      par[v] = up[0][v] = p;
      if (whoS[v] != -1) {
        fakeS[0][v] = whoS[v];
      }
      if (whoT[v] != -1) {
        fakeT[0][v] = whoT[v];
      }
      for (int k = 1; k < 32; ++k) {
        int mid = up[k - 1][v];
        if (mid == -1) {
          break;
        }
        up[k][v] = up[k - 1][mid];
        add_edge(fakeS[k - 1][v], m);
        add_edge(fakeS[k - 1][mid], m);
        fakeS[k][v] = m++;
        add_edge(m, fakeT[k - 1][v]);
        add_edge(m, fakeT[k - 1][mid]);
        fakeT[k][v] = m++;
      }
      for (int u : t[v]) {
        if (u != p) {
          dep[u] = dep[v] + 1;
          self(self, u, v);
        }
      }
    };
    dfs(dfs, 0, -1);

    auto jump = [&](int& at, int x) {
      for (int k = 0; k < 32; ++k) {
        assert(at != -1);
        if (x >> k & 1) {
          at = up[k][at];
        }
      }
    };
    auto LCA = [&](int a, int b) {
      if (dep[a] < dep[b]) {
        swap(a, b);
      }
      jump(a, dep[a] - dep[b]);
      if (a == b) {
        return a;
      }
      for (int t = 32 - 1; t >= 0; --t) {
        int na = up[t][a];
        int nb = up[t][b];
        if (na != nb) {
          a = na;
          b = nb;
        }
      }
      return par[a];
    };

    for (int i = 0; i < M; ++i) {
      int lca = LCA(S[i], T[i]);
      for (int x : {S[i], T[i]}) {
        if (x == lca) {
          continue;
        }
        x = par[x];
        for (int t = 32 - 1; t >= 0; --t) {
          int y = up[t][x];
          if (y != -1 && dep[y] >= dep[lca]) {
            add_edge(fakeS[t][x], i);
            add_edge(i, fakeT[t][x]);
            x = y;
          }
        }
      }
      add_edge(whoS[lca], i);
      add_edge(whoS[T[i]], i);
      add_edge(i, whoT[lca]);
      add_edge(i, whoT[S[i]]);
    }

    vector<int> vis(m);
    bool fail = false;
    auto check = [&](auto self, int v) -> void {
      vis[v] = 1;
      for (int u : d[v]) {
        if (!vis[u]) {
          self(self, u);
        } else if (vis[u] == 1) {
          fail = true;
        }
      }
      vis[v] = 2;
    };
    for (int i = 0; i < m; ++i) {
      if (!vis[i]) {
        check(check, i);
      }
    }
    cout << (fail ? "No" : "Yes") << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 65 ms 732 KB Output is correct
5 Correct 122 ms 1296 KB Output is correct
6 Correct 7 ms 1084 KB Output is correct
7 Correct 9 ms 1180 KB Output is correct
8 Correct 9 ms 1204 KB Output is correct
9 Correct 392 ms 30316 KB Output is correct
10 Correct 1215 ms 387828 KB Output is correct
11 Correct 21 ms 600 KB Output is correct
12 Correct 156 ms 1816 KB Output is correct
13 Correct 1229 ms 476152 KB Output is correct
14 Correct 1081 ms 474696 KB Output is correct
15 Correct 1603 ms 475236 KB Output is correct
16 Correct 2227 ms 562220 KB Output is correct
17 Correct 1449 ms 558252 KB Output is correct
18 Correct 1275 ms 561696 KB Output is correct
19 Correct 1322 ms 557660 KB Output is correct
20 Correct 1365 ms 558100 KB Output is correct
21 Correct 1381 ms 558252 KB Output is correct
22 Correct 1031 ms 560284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 1064 KB Output is correct
4 Correct 6 ms 960 KB Output is correct
5 Correct 5 ms 956 KB Output is correct
6 Correct 6 ms 948 KB Output is correct
7 Correct 5 ms 956 KB Output is correct
8 Correct 5 ms 940 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 972 KB Output is correct
11 Correct 5 ms 952 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 1064 KB Output is correct
4 Correct 6 ms 960 KB Output is correct
5 Correct 5 ms 956 KB Output is correct
6 Correct 6 ms 948 KB Output is correct
7 Correct 5 ms 956 KB Output is correct
8 Correct 5 ms 940 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 972 KB Output is correct
11 Correct 5 ms 952 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 8 ms 1424 KB Output is correct
17 Correct 5 ms 788 KB Output is correct
18 Correct 7 ms 944 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 7 ms 952 KB Output is correct
21 Correct 7 ms 1132 KB Output is correct
22 Correct 4 ms 872 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 6 ms 912 KB Output is correct
26 Correct 1 ms 752 KB Output is correct
27 Correct 4 ms 860 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 1064 KB Output is correct
4 Correct 6 ms 960 KB Output is correct
5 Correct 5 ms 956 KB Output is correct
6 Correct 6 ms 948 KB Output is correct
7 Correct 5 ms 956 KB Output is correct
8 Correct 5 ms 940 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 972 KB Output is correct
11 Correct 5 ms 952 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 8 ms 1424 KB Output is correct
17 Correct 5 ms 788 KB Output is correct
18 Correct 7 ms 944 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 7 ms 952 KB Output is correct
21 Correct 7 ms 1132 KB Output is correct
22 Correct 4 ms 872 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 6 ms 912 KB Output is correct
26 Correct 1 ms 752 KB Output is correct
27 Correct 4 ms 860 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 9 ms 1208 KB Output is correct
30 Correct 4 ms 864 KB Output is correct
31 Correct 7 ms 1080 KB Output is correct
32 Correct 9 ms 1140 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 7 ms 920 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 3 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 7 ms 1064 KB Output is correct
4 Correct 6 ms 960 KB Output is correct
5 Correct 5 ms 956 KB Output is correct
6 Correct 6 ms 948 KB Output is correct
7 Correct 5 ms 956 KB Output is correct
8 Correct 5 ms 940 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 972 KB Output is correct
11 Correct 5 ms 952 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 8 ms 1424 KB Output is correct
17 Correct 5 ms 788 KB Output is correct
18 Correct 7 ms 944 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 7 ms 952 KB Output is correct
21 Correct 7 ms 1132 KB Output is correct
22 Correct 4 ms 872 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 6 ms 912 KB Output is correct
26 Correct 1 ms 752 KB Output is correct
27 Correct 4 ms 860 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 9 ms 1208 KB Output is correct
30 Correct 4 ms 864 KB Output is correct
31 Correct 7 ms 1080 KB Output is correct
32 Correct 9 ms 1140 KB Output is correct
33 Correct 5 ms 860 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 7 ms 920 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 3 ms 772 KB Output is correct
38 Correct 393 ms 27284 KB Output is correct
39 Correct 1180 ms 387508 KB Output is correct
40 Correct 189 ms 20348 KB Output is correct
41 Correct 95 ms 9708 KB Output is correct
42 Correct 117 ms 15040 KB Output is correct
43 Correct 283 ms 23752 KB Output is correct
44 Correct 20 ms 1916 KB Output is correct
45 Correct 373 ms 179840 KB Output is correct
46 Correct 337 ms 181696 KB Output is correct
47 Correct 923 ms 355096 KB Output is correct
48 Correct 930 ms 355788 KB Output is correct
49 Correct 466 ms 311356 KB Output is correct
50 Correct 444 ms 310380 KB Output is correct
51 Correct 605 ms 320496 KB Output is correct
52 Correct 612 ms 320880 KB Output is correct
53 Correct 28 ms 13452 KB Output is correct
54 Correct 315 ms 180928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 21 ms 344 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 652 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 4 ms 604 KB Output is correct
13 Correct 59 ms 1120 KB Output is correct
14 Correct 87 ms 1364 KB Output is correct
15 Correct 72 ms 1104 KB Output is correct
16 Correct 252 ms 133124 KB Output is correct
17 Correct 416 ms 137316 KB Output is correct
18 Correct 644 ms 178080 KB Output is correct
19 Correct 290 ms 132564 KB Output is correct
20 Correct 278 ms 132656 KB Output is correct
21 Correct 281 ms 132380 KB Output is correct
22 Correct 322 ms 133396 KB Output is correct
23 Correct 344 ms 131832 KB Output is correct
24 Correct 280 ms 131084 KB Output is correct
25 Correct 298 ms 133344 KB Output is correct
26 Correct 277 ms 132112 KB Output is correct
27 Correct 350 ms 116220 KB Output is correct
28 Correct 299 ms 115708 KB Output is correct
29 Correct 353 ms 115672 KB Output is correct
30 Correct 213 ms 114304 KB Output is correct
31 Correct 216 ms 115032 KB Output is correct
32 Correct 242 ms 113300 KB Output is correct
33 Correct 243 ms 112828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 65 ms 732 KB Output is correct
5 Correct 122 ms 1296 KB Output is correct
6 Correct 7 ms 1084 KB Output is correct
7 Correct 9 ms 1180 KB Output is correct
8 Correct 9 ms 1204 KB Output is correct
9 Correct 392 ms 30316 KB Output is correct
10 Correct 1215 ms 387828 KB Output is correct
11 Correct 21 ms 600 KB Output is correct
12 Correct 156 ms 1816 KB Output is correct
13 Correct 1229 ms 476152 KB Output is correct
14 Correct 1081 ms 474696 KB Output is correct
15 Correct 1603 ms 475236 KB Output is correct
16 Correct 2227 ms 562220 KB Output is correct
17 Correct 1449 ms 558252 KB Output is correct
18 Correct 1275 ms 561696 KB Output is correct
19 Correct 1322 ms 557660 KB Output is correct
20 Correct 1365 ms 558100 KB Output is correct
21 Correct 1381 ms 558252 KB Output is correct
22 Correct 1031 ms 560284 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 7 ms 1064 KB Output is correct
26 Correct 6 ms 960 KB Output is correct
27 Correct 5 ms 956 KB Output is correct
28 Correct 6 ms 948 KB Output is correct
29 Correct 5 ms 956 KB Output is correct
30 Correct 5 ms 940 KB Output is correct
31 Correct 6 ms 972 KB Output is correct
32 Correct 6 ms 972 KB Output is correct
33 Correct 5 ms 952 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 8 ms 1424 KB Output is correct
39 Correct 5 ms 788 KB Output is correct
40 Correct 7 ms 944 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 7 ms 952 KB Output is correct
43 Correct 7 ms 1132 KB Output is correct
44 Correct 4 ms 872 KB Output is correct
45 Correct 1 ms 344 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 6 ms 912 KB Output is correct
48 Correct 1 ms 752 KB Output is correct
49 Correct 4 ms 860 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 9 ms 1208 KB Output is correct
52 Correct 4 ms 864 KB Output is correct
53 Correct 7 ms 1080 KB Output is correct
54 Correct 9 ms 1140 KB Output is correct
55 Correct 5 ms 860 KB Output is correct
56 Correct 4 ms 604 KB Output is correct
57 Correct 7 ms 920 KB Output is correct
58 Correct 3 ms 600 KB Output is correct
59 Correct 3 ms 772 KB Output is correct
60 Correct 393 ms 27284 KB Output is correct
61 Correct 1180 ms 387508 KB Output is correct
62 Correct 189 ms 20348 KB Output is correct
63 Correct 95 ms 9708 KB Output is correct
64 Correct 117 ms 15040 KB Output is correct
65 Correct 283 ms 23752 KB Output is correct
66 Correct 20 ms 1916 KB Output is correct
67 Correct 373 ms 179840 KB Output is correct
68 Correct 337 ms 181696 KB Output is correct
69 Correct 923 ms 355096 KB Output is correct
70 Correct 930 ms 355788 KB Output is correct
71 Correct 466 ms 311356 KB Output is correct
72 Correct 444 ms 310380 KB Output is correct
73 Correct 605 ms 320496 KB Output is correct
74 Correct 612 ms 320880 KB Output is correct
75 Correct 28 ms 13452 KB Output is correct
76 Correct 315 ms 180928 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 21 ms 344 KB Output is correct
82 Correct 1 ms 604 KB Output is correct
83 Correct 2 ms 604 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 1 ms 348 KB Output is correct
86 Correct 1 ms 652 KB Output is correct
87 Correct 1 ms 348 KB Output is correct
88 Correct 4 ms 604 KB Output is correct
89 Correct 59 ms 1120 KB Output is correct
90 Correct 87 ms 1364 KB Output is correct
91 Correct 72 ms 1104 KB Output is correct
92 Correct 252 ms 133124 KB Output is correct
93 Correct 416 ms 137316 KB Output is correct
94 Correct 644 ms 178080 KB Output is correct
95 Correct 290 ms 132564 KB Output is correct
96 Correct 278 ms 132656 KB Output is correct
97 Correct 281 ms 132380 KB Output is correct
98 Correct 322 ms 133396 KB Output is correct
99 Correct 344 ms 131832 KB Output is correct
100 Correct 280 ms 131084 KB Output is correct
101 Correct 298 ms 133344 KB Output is correct
102 Correct 277 ms 132112 KB Output is correct
103 Correct 350 ms 116220 KB Output is correct
104 Correct 299 ms 115708 KB Output is correct
105 Correct 353 ms 115672 KB Output is correct
106 Correct 213 ms 114304 KB Output is correct
107 Correct 216 ms 115032 KB Output is correct
108 Correct 242 ms 113300 KB Output is correct
109 Correct 243 ms 112828 KB Output is correct
110 Correct 96 ms 1700 KB Output is correct
111 Correct 91 ms 1428 KB Output is correct
112 Correct 1426 ms 386880 KB Output is correct
113 Correct 1113 ms 382816 KB Output is correct
114 Correct 820 ms 306636 KB Output is correct
115 Correct 67 ms 74572 KB Output is correct
116 Correct 369 ms 176756 KB Output is correct
117 Correct 838 ms 187048 KB Output is correct
118 Correct 290 ms 179600 KB Output is correct
119 Correct 273 ms 180596 KB Output is correct
120 Correct 23 ms 16280 KB Output is correct
121 Correct 542 ms 182444 KB Output is correct
122 Correct 536 ms 176140 KB Output is correct
123 Correct 1298 ms 452164 KB Output is correct
124 Correct 1234 ms 450424 KB Output is correct
125 Correct 1314 ms 500044 KB Output is correct
126 Correct 1963 ms 434940 KB Output is correct
127 Correct 1257 ms 378532 KB Output is correct
128 Correct 1106 ms 380136 KB Output is correct
129 Correct 1064 ms 362544 KB Output is correct
130 Correct 1110 ms 375748 KB Output is correct