Submission #867167

# Submission time Handle Problem Language Result Execution time Memory
867167 2023-10-27T19:34:52 Z nima_aryan Jail (JOI22_jail) C++17
100 / 100
2053 ms 479304 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;

    const int logN = log2(N) + 1;

    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(logN, 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(logN, vector<int>(N, -1));
    vector fakeT(logN, 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 < logN; ++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 < logN; ++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 = logN - 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 = logN - 1; t >= 0; --t) {
          int y = up[t][x];
          if (y != -1 && dep[y] >= dep[lca]) {  // `=` for `fake[t][x] + 1 = up[t][x]`
            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 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 53 ms 684 KB Output is correct
5 Correct 112 ms 604 KB Output is correct
6 Correct 7 ms 932 KB Output is correct
7 Correct 7 ms 1048 KB Output is correct
8 Correct 9 ms 1064 KB Output is correct
9 Correct 382 ms 25180 KB Output is correct
10 Correct 1063 ms 357652 KB Output is correct
11 Correct 18 ms 348 KB Output is correct
12 Correct 150 ms 732 KB Output is correct
13 Correct 1112 ms 455324 KB Output is correct
14 Correct 1029 ms 455168 KB Output is correct
15 Correct 1526 ms 455268 KB Output is correct
16 Correct 2053 ms 414260 KB Output is correct
17 Correct 1302 ms 368548 KB Output is correct
18 Correct 1054 ms 375608 KB Output is correct
19 Correct 1246 ms 369020 KB Output is correct
20 Correct 1275 ms 369404 KB Output is correct
21 Correct 1278 ms 369948 KB Output is correct
22 Correct 913 ms 367144 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 7 ms 936 KB Output is correct
4 Correct 6 ms 856 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
6 Correct 5 ms 840 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 848 KB Output is correct
9 Correct 5 ms 852 KB Output is correct
10 Correct 5 ms 844 KB Output is correct
11 Correct 5 ms 884 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 2 ms 680 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 7 ms 936 KB Output is correct
4 Correct 6 ms 856 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
6 Correct 5 ms 840 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 848 KB Output is correct
9 Correct 5 ms 852 KB Output is correct
10 Correct 5 ms 844 KB Output is correct
11 Correct 5 ms 884 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 2 ms 680 KB Output is correct
14 Correct 0 ms 512 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 9 ms 1136 KB Output is correct
17 Correct 3 ms 664 KB Output is correct
18 Correct 7 ms 876 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 6 ms 876 KB Output is correct
21 Correct 6 ms 884 KB Output is correct
22 Correct 4 ms 760 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 6 ms 836 KB Output is correct
26 Correct 1 ms 752 KB Output is correct
27 Correct 3 ms 752 KB Output is correct
28 Correct 1 ms 348 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 7 ms 936 KB Output is correct
4 Correct 6 ms 856 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
6 Correct 5 ms 840 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 848 KB Output is correct
9 Correct 5 ms 852 KB Output is correct
10 Correct 5 ms 844 KB Output is correct
11 Correct 5 ms 884 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 2 ms 680 KB Output is correct
14 Correct 0 ms 512 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 9 ms 1136 KB Output is correct
17 Correct 3 ms 664 KB Output is correct
18 Correct 7 ms 876 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 6 ms 876 KB Output is correct
21 Correct 6 ms 884 KB Output is correct
22 Correct 4 ms 760 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 6 ms 836 KB Output is correct
26 Correct 1 ms 752 KB Output is correct
27 Correct 3 ms 752 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 9 ms 1064 KB Output is correct
30 Correct 4 ms 732 KB Output is correct
31 Correct 7 ms 940 KB Output is correct
32 Correct 7 ms 972 KB Output is correct
33 Correct 6 ms 772 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 6 ms 816 KB Output is correct
36 Correct 3 ms 604 KB Output is correct
37 Correct 3 ms 776 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 7 ms 936 KB Output is correct
4 Correct 6 ms 856 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
6 Correct 5 ms 840 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 848 KB Output is correct
9 Correct 5 ms 852 KB Output is correct
10 Correct 5 ms 844 KB Output is correct
11 Correct 5 ms 884 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 2 ms 680 KB Output is correct
14 Correct 0 ms 512 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 9 ms 1136 KB Output is correct
17 Correct 3 ms 664 KB Output is correct
18 Correct 7 ms 876 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 6 ms 876 KB Output is correct
21 Correct 6 ms 884 KB Output is correct
22 Correct 4 ms 760 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 6 ms 836 KB Output is correct
26 Correct 1 ms 752 KB Output is correct
27 Correct 3 ms 752 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 9 ms 1064 KB Output is correct
30 Correct 4 ms 732 KB Output is correct
31 Correct 7 ms 940 KB Output is correct
32 Correct 7 ms 972 KB Output is correct
33 Correct 6 ms 772 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 6 ms 816 KB Output is correct
36 Correct 3 ms 604 KB Output is correct
37 Correct 3 ms 776 KB Output is correct
38 Correct 381 ms 24672 KB Output is correct
39 Correct 1113 ms 358792 KB Output is correct
40 Correct 184 ms 16920 KB Output is correct
41 Correct 93 ms 8476 KB Output is correct
42 Correct 96 ms 13556 KB Output is correct
43 Correct 302 ms 23104 KB Output is correct
44 Correct 18 ms 1404 KB Output is correct
45 Correct 345 ms 157596 KB Output is correct
46 Correct 315 ms 159420 KB Output is correct
47 Correct 928 ms 334180 KB Output is correct
48 Correct 901 ms 334384 KB Output is correct
49 Correct 425 ms 288056 KB Output is correct
50 Correct 442 ms 288316 KB Output is correct
51 Correct 592 ms 298576 KB Output is correct
52 Correct 598 ms 299936 KB Output is correct
53 Correct 27 ms 11380 KB Output is correct
54 Correct 276 ms 157632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 17 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 3 ms 676 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 50 ms 592 KB Output is correct
14 Correct 80 ms 608 KB Output is correct
15 Correct 70 ms 596 KB Output is correct
16 Correct 213 ms 109780 KB Output is correct
17 Correct 368 ms 115212 KB Output is correct
18 Correct 541 ms 155288 KB Output is correct
19 Correct 245 ms 110844 KB Output is correct
20 Correct 238 ms 111064 KB Output is correct
21 Correct 268 ms 111188 KB Output is correct
22 Correct 277 ms 109848 KB Output is correct
23 Correct 245 ms 108568 KB Output is correct
24 Correct 263 ms 108560 KB Output is correct
25 Correct 251 ms 110424 KB Output is correct
26 Correct 257 ms 110096 KB Output is correct
27 Correct 232 ms 93440 KB Output is correct
28 Correct 249 ms 94260 KB Output is correct
29 Correct 252 ms 92760 KB Output is correct
30 Correct 180 ms 91552 KB Output is correct
31 Correct 192 ms 92796 KB Output is correct
32 Correct 202 ms 91260 KB Output is correct
33 Correct 190 ms 90680 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 53 ms 684 KB Output is correct
5 Correct 112 ms 604 KB Output is correct
6 Correct 7 ms 932 KB Output is correct
7 Correct 7 ms 1048 KB Output is correct
8 Correct 9 ms 1064 KB Output is correct
9 Correct 382 ms 25180 KB Output is correct
10 Correct 1063 ms 357652 KB Output is correct
11 Correct 18 ms 348 KB Output is correct
12 Correct 150 ms 732 KB Output is correct
13 Correct 1112 ms 455324 KB Output is correct
14 Correct 1029 ms 455168 KB Output is correct
15 Correct 1526 ms 455268 KB Output is correct
16 Correct 2053 ms 414260 KB Output is correct
17 Correct 1302 ms 368548 KB Output is correct
18 Correct 1054 ms 375608 KB Output is correct
19 Correct 1246 ms 369020 KB Output is correct
20 Correct 1275 ms 369404 KB Output is correct
21 Correct 1278 ms 369948 KB Output is correct
22 Correct 913 ms 367144 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 7 ms 936 KB Output is correct
26 Correct 6 ms 856 KB Output is correct
27 Correct 5 ms 880 KB Output is correct
28 Correct 5 ms 840 KB Output is correct
29 Correct 5 ms 852 KB Output is correct
30 Correct 5 ms 848 KB Output is correct
31 Correct 5 ms 852 KB Output is correct
32 Correct 5 ms 844 KB Output is correct
33 Correct 5 ms 884 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 2 ms 680 KB Output is correct
36 Correct 0 ms 512 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 9 ms 1136 KB Output is correct
39 Correct 3 ms 664 KB Output is correct
40 Correct 7 ms 876 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 6 ms 876 KB Output is correct
43 Correct 6 ms 884 KB Output is correct
44 Correct 4 ms 760 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 6 ms 836 KB Output is correct
48 Correct 1 ms 752 KB Output is correct
49 Correct 3 ms 752 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 9 ms 1064 KB Output is correct
52 Correct 4 ms 732 KB Output is correct
53 Correct 7 ms 940 KB Output is correct
54 Correct 7 ms 972 KB Output is correct
55 Correct 6 ms 772 KB Output is correct
56 Correct 4 ms 604 KB Output is correct
57 Correct 6 ms 816 KB Output is correct
58 Correct 3 ms 604 KB Output is correct
59 Correct 3 ms 776 KB Output is correct
60 Correct 381 ms 24672 KB Output is correct
61 Correct 1113 ms 358792 KB Output is correct
62 Correct 184 ms 16920 KB Output is correct
63 Correct 93 ms 8476 KB Output is correct
64 Correct 96 ms 13556 KB Output is correct
65 Correct 302 ms 23104 KB Output is correct
66 Correct 18 ms 1404 KB Output is correct
67 Correct 345 ms 157596 KB Output is correct
68 Correct 315 ms 159420 KB Output is correct
69 Correct 928 ms 334180 KB Output is correct
70 Correct 901 ms 334384 KB Output is correct
71 Correct 425 ms 288056 KB Output is correct
72 Correct 442 ms 288316 KB Output is correct
73 Correct 592 ms 298576 KB Output is correct
74 Correct 598 ms 299936 KB Output is correct
75 Correct 27 ms 11380 KB Output is correct
76 Correct 276 ms 157632 KB Output is correct
77 Correct 0 ms 344 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 1 ms 344 KB Output is correct
80 Correct 0 ms 344 KB Output is correct
81 Correct 17 ms 348 KB Output is correct
82 Correct 1 ms 348 KB Output is correct
83 Correct 3 ms 676 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 0 ms 348 KB Output is correct
86 Correct 1 ms 348 KB Output is correct
87 Correct 1 ms 348 KB Output is correct
88 Correct 3 ms 604 KB Output is correct
89 Correct 50 ms 592 KB Output is correct
90 Correct 80 ms 608 KB Output is correct
91 Correct 70 ms 596 KB Output is correct
92 Correct 213 ms 109780 KB Output is correct
93 Correct 368 ms 115212 KB Output is correct
94 Correct 541 ms 155288 KB Output is correct
95 Correct 245 ms 110844 KB Output is correct
96 Correct 238 ms 111064 KB Output is correct
97 Correct 268 ms 111188 KB Output is correct
98 Correct 277 ms 109848 KB Output is correct
99 Correct 245 ms 108568 KB Output is correct
100 Correct 263 ms 108560 KB Output is correct
101 Correct 251 ms 110424 KB Output is correct
102 Correct 257 ms 110096 KB Output is correct
103 Correct 232 ms 93440 KB Output is correct
104 Correct 249 ms 94260 KB Output is correct
105 Correct 252 ms 92760 KB Output is correct
106 Correct 180 ms 91552 KB Output is correct
107 Correct 192 ms 92796 KB Output is correct
108 Correct 202 ms 91260 KB Output is correct
109 Correct 190 ms 90680 KB Output is correct
110 Correct 92 ms 704 KB Output is correct
111 Correct 81 ms 648 KB Output is correct
112 Correct 1408 ms 363860 KB Output is correct
113 Correct 1086 ms 361552 KB Output is correct
114 Correct 773 ms 285244 KB Output is correct
115 Correct 47 ms 51528 KB Output is correct
116 Correct 343 ms 154448 KB Output is correct
117 Correct 664 ms 163776 KB Output is correct
118 Correct 294 ms 158080 KB Output is correct
119 Correct 268 ms 157228 KB Output is correct
120 Correct 20 ms 13972 KB Output is correct
121 Correct 542 ms 158816 KB Output is correct
122 Correct 495 ms 153372 KB Output is correct
123 Correct 1233 ms 431096 KB Output is correct
124 Correct 1144 ms 431100 KB Output is correct
125 Correct 1235 ms 479304 KB Output is correct
126 Correct 1875 ms 407444 KB Output is correct
127 Correct 1157 ms 355600 KB Output is correct
128 Correct 1091 ms 356364 KB Output is correct
129 Correct 1014 ms 339128 KB Output is correct
130 Correct 1092 ms 353216 KB Output is correct