답안 #1039081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039081 2024-07-30T12:36:19 Z juicy Jail (JOI22_jail) C++17
100 / 100
856 ms 277592 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 12e4 + 5, LG = 17, MAX = 4080005;

int n, m, q, tot;
int dep[N], pr[LG][N], S[LG][N], T[LG][N], vis[MAX];
vector<int> g[N], gph[MAX];

void add(int u, int v) {
  gph[u].push_back(v);
}

void create(int j, int v) {
  if (!S[j][v]) {
    S[j][v] = ++tot;
  }
  if (!T[j][v]) {
    T[j][v] = ++tot;
  }
  if (j > 0) {
    add(S[j - 1][v], S[j][v]);
    add(S[j - 1][pr[j - 1][v]], S[j][v]);
    add(T[j][v], T[j - 1][v]);
    add(T[j][v], T[j - 1][pr[j - 1][v]]);
  }
} 

void dfs(int u) {
  for (int v : g[u]) {
    if (v != pr[0][u]) {
      dep[v] = dep[u] + 1;
      pr[0][v] = u;
      for (int j = 1; j < LG; ++j) {
        pr[j][v] = pr[j - 1][pr[j - 1][v]];
      }
      dfs(v);
    }
  }
}

void adds(int u, int p, int s) {
  for (int j = LG - 1; ~j; --j) {
    if (dep[u] - (1 << j) >= dep[p]) {
      add(S[j][u], S[0][s]);
      u = pr[j][u];
    }
  } 
}

void addt(int u, int p, int t) {
  for (int j = LG - 1; ~j; --j) {
    if (dep[u] - (1 << j) >= dep[p]) {
      add(T[0][t], T[j][u]);
      u = pr[j][u];
    }
  }
}

int lca(int u, int v) {
  if (dep[u] < dep[v]) {
    swap(u, v);
  }
  for (int j = LG - 1; ~j; --j) {
    if (dep[u] - (1 << j) >= dep[v]) {
      u = pr[j][u];
    }
  }
  if (u == v) {
    return u;
  }
  for (int j = LG - 1; ~j; --j) {
    if (pr[j][u] != pr[j][v]) {
      u = pr[j][u], v = pr[j][v];
    }
  }
  return pr[0][u];
}

bool cyc;

void DFS(int u) {
  vis[u] = 1;
  for (int v : gph[u]) {
    if (!vis[v]) {
      DFS(v);
      if (cyc) {
        return;
      }
    } else if (vis[v] == 1) {
      cyc = 1;
      return;
    }
  }
  vis[u] = 2;
}

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

  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i < n; ++i) {
      int u, v; cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    dep[1] = 1;
    dfs(1);
    cin >> m;
    vector<array<int, 2>> dat(m);
    for (int i = 1; i <= m; ++i) {
      int s, t; cin >> s >> t;
      dat[i - 1] = {s, t};
      S[0][s] = T[0][t] = i;
    }
    tot = m;
    for (int j = 0; j < LG; ++j) {
      for (int i = 1; i <= n; ++i) {
        if (dep[i] >= (1 << j)) {
          create(j, i);
        }
      }
    }
    for (int i = 1; i <= m; ++i) {
      auto [s, t] = dat[i - 1];
      int p = lca(s, t);
      adds(pr[0][s], p, s);
      adds(t, p == s ? s : pr[0][p], s);
      addt(pr[0][t], p, t);
      addt(s, p == t ? t : pr[0][p], t);
    }
    for (int i = 1; i <= tot && !cyc; ++i) {
      if (!vis[i]) {
        DFS(i);
      }
    }
    cout << (cyc ? "No" : "Yes") << "\n";
    cyc = 0;
    for (int i = 1; i <= n; ++i) {
      g[i].clear();
      for (int j = 0; j < LG; ++j) {
        S[j][i] = T[j][i] = 0;
      }
    }
    while (tot > 0) {
      gph[tot].clear();
      vis[tot] = 0;
      --tot;
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 99152 KB Output is correct
2 Correct 47 ms 99068 KB Output is correct
3 Correct 40 ms 99152 KB Output is correct
4 Correct 54 ms 99664 KB Output is correct
5 Correct 66 ms 100048 KB Output is correct
6 Correct 52 ms 99340 KB Output is correct
7 Correct 41 ms 99376 KB Output is correct
8 Correct 51 ms 99408 KB Output is correct
9 Correct 123 ms 107020 KB Output is correct
10 Correct 505 ms 265044 KB Output is correct
11 Correct 43 ms 99408 KB Output is correct
12 Correct 87 ms 100176 KB Output is correct
13 Correct 506 ms 268116 KB Output is correct
14 Correct 378 ms 268180 KB Output is correct
15 Correct 538 ms 269748 KB Output is correct
16 Correct 786 ms 277592 KB Output is correct
17 Correct 480 ms 270724 KB Output is correct
18 Correct 412 ms 266916 KB Output is correct
19 Correct 444 ms 270724 KB Output is correct
20 Correct 349 ms 270852 KB Output is correct
21 Correct 394 ms 270672 KB Output is correct
22 Correct 300 ms 266068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 99284 KB Output is correct
2 Correct 39 ms 99156 KB Output is correct
3 Correct 45 ms 99416 KB Output is correct
4 Correct 41 ms 99412 KB Output is correct
5 Correct 42 ms 99420 KB Output is correct
6 Correct 41 ms 99412 KB Output is correct
7 Correct 42 ms 99412 KB Output is correct
8 Correct 40 ms 99420 KB Output is correct
9 Correct 50 ms 99400 KB Output is correct
10 Correct 39 ms 99416 KB Output is correct
11 Correct 39 ms 99412 KB Output is correct
12 Correct 38 ms 99420 KB Output is correct
13 Correct 36 ms 99420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 99284 KB Output is correct
2 Correct 39 ms 99156 KB Output is correct
3 Correct 45 ms 99416 KB Output is correct
4 Correct 41 ms 99412 KB Output is correct
5 Correct 42 ms 99420 KB Output is correct
6 Correct 41 ms 99412 KB Output is correct
7 Correct 42 ms 99412 KB Output is correct
8 Correct 40 ms 99420 KB Output is correct
9 Correct 50 ms 99400 KB Output is correct
10 Correct 39 ms 99416 KB Output is correct
11 Correct 39 ms 99412 KB Output is correct
12 Correct 38 ms 99420 KB Output is correct
13 Correct 36 ms 99420 KB Output is correct
14 Correct 39 ms 99072 KB Output is correct
15 Correct 44 ms 99152 KB Output is correct
16 Correct 42 ms 99408 KB Output is correct
17 Correct 40 ms 99412 KB Output is correct
18 Correct 41 ms 99364 KB Output is correct
19 Correct 38 ms 99160 KB Output is correct
20 Correct 41 ms 99416 KB Output is correct
21 Correct 40 ms 99408 KB Output is correct
22 Correct 40 ms 99420 KB Output is correct
23 Correct 40 ms 99152 KB Output is correct
24 Correct 45 ms 99152 KB Output is correct
25 Correct 39 ms 99404 KB Output is correct
26 Correct 37 ms 99416 KB Output is correct
27 Correct 48 ms 99260 KB Output is correct
28 Correct 37 ms 99292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 99284 KB Output is correct
2 Correct 39 ms 99156 KB Output is correct
3 Correct 45 ms 99416 KB Output is correct
4 Correct 41 ms 99412 KB Output is correct
5 Correct 42 ms 99420 KB Output is correct
6 Correct 41 ms 99412 KB Output is correct
7 Correct 42 ms 99412 KB Output is correct
8 Correct 40 ms 99420 KB Output is correct
9 Correct 50 ms 99400 KB Output is correct
10 Correct 39 ms 99416 KB Output is correct
11 Correct 39 ms 99412 KB Output is correct
12 Correct 38 ms 99420 KB Output is correct
13 Correct 36 ms 99420 KB Output is correct
14 Correct 39 ms 99072 KB Output is correct
15 Correct 44 ms 99152 KB Output is correct
16 Correct 42 ms 99408 KB Output is correct
17 Correct 40 ms 99412 KB Output is correct
18 Correct 41 ms 99364 KB Output is correct
19 Correct 38 ms 99160 KB Output is correct
20 Correct 41 ms 99416 KB Output is correct
21 Correct 40 ms 99408 KB Output is correct
22 Correct 40 ms 99420 KB Output is correct
23 Correct 40 ms 99152 KB Output is correct
24 Correct 45 ms 99152 KB Output is correct
25 Correct 39 ms 99404 KB Output is correct
26 Correct 37 ms 99416 KB Output is correct
27 Correct 48 ms 99260 KB Output is correct
28 Correct 37 ms 99292 KB Output is correct
29 Correct 49 ms 99484 KB Output is correct
30 Correct 42 ms 99416 KB Output is correct
31 Correct 38 ms 99560 KB Output is correct
32 Correct 39 ms 99412 KB Output is correct
33 Correct 47 ms 99404 KB Output is correct
34 Correct 40 ms 99416 KB Output is correct
35 Correct 41 ms 99364 KB Output is correct
36 Correct 44 ms 99496 KB Output is correct
37 Correct 48 ms 99412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 99284 KB Output is correct
2 Correct 39 ms 99156 KB Output is correct
3 Correct 45 ms 99416 KB Output is correct
4 Correct 41 ms 99412 KB Output is correct
5 Correct 42 ms 99420 KB Output is correct
6 Correct 41 ms 99412 KB Output is correct
7 Correct 42 ms 99412 KB Output is correct
8 Correct 40 ms 99420 KB Output is correct
9 Correct 50 ms 99400 KB Output is correct
10 Correct 39 ms 99416 KB Output is correct
11 Correct 39 ms 99412 KB Output is correct
12 Correct 38 ms 99420 KB Output is correct
13 Correct 36 ms 99420 KB Output is correct
14 Correct 39 ms 99072 KB Output is correct
15 Correct 44 ms 99152 KB Output is correct
16 Correct 42 ms 99408 KB Output is correct
17 Correct 40 ms 99412 KB Output is correct
18 Correct 41 ms 99364 KB Output is correct
19 Correct 38 ms 99160 KB Output is correct
20 Correct 41 ms 99416 KB Output is correct
21 Correct 40 ms 99408 KB Output is correct
22 Correct 40 ms 99420 KB Output is correct
23 Correct 40 ms 99152 KB Output is correct
24 Correct 45 ms 99152 KB Output is correct
25 Correct 39 ms 99404 KB Output is correct
26 Correct 37 ms 99416 KB Output is correct
27 Correct 48 ms 99260 KB Output is correct
28 Correct 37 ms 99292 KB Output is correct
29 Correct 49 ms 99484 KB Output is correct
30 Correct 42 ms 99416 KB Output is correct
31 Correct 38 ms 99560 KB Output is correct
32 Correct 39 ms 99412 KB Output is correct
33 Correct 47 ms 99404 KB Output is correct
34 Correct 40 ms 99416 KB Output is correct
35 Correct 41 ms 99364 KB Output is correct
36 Correct 44 ms 99496 KB Output is correct
37 Correct 48 ms 99412 KB Output is correct
38 Correct 92 ms 107092 KB Output is correct
39 Correct 464 ms 265044 KB Output is correct
40 Correct 85 ms 107344 KB Output is correct
41 Correct 76 ms 103748 KB Output is correct
42 Correct 64 ms 105300 KB Output is correct
43 Correct 104 ms 107344 KB Output is correct
44 Correct 45 ms 99872 KB Output is correct
45 Correct 178 ms 176496 KB Output is correct
46 Correct 228 ms 176972 KB Output is correct
47 Correct 522 ms 251952 KB Output is correct
48 Correct 515 ms 252332 KB Output is correct
49 Correct 248 ms 213840 KB Output is correct
50 Correct 302 ms 213716 KB Output is correct
51 Correct 367 ms 239700 KB Output is correct
52 Correct 297 ms 240008 KB Output is correct
53 Correct 61 ms 104528 KB Output is correct
54 Correct 279 ms 168528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 99164 KB Output is correct
2 Correct 41 ms 99152 KB Output is correct
3 Correct 39 ms 99152 KB Output is correct
4 Correct 41 ms 99152 KB Output is correct
5 Correct 46 ms 99412 KB Output is correct
6 Correct 42 ms 99408 KB Output is correct
7 Correct 42 ms 99412 KB Output is correct
8 Correct 40 ms 99324 KB Output is correct
9 Correct 40 ms 99152 KB Output is correct
10 Correct 48 ms 99220 KB Output is correct
11 Correct 40 ms 99328 KB Output is correct
12 Correct 43 ms 99420 KB Output is correct
13 Correct 60 ms 99924 KB Output is correct
14 Correct 73 ms 100180 KB Output is correct
15 Correct 76 ms 99924 KB Output is correct
16 Correct 168 ms 156240 KB Output is correct
17 Correct 247 ms 160084 KB Output is correct
18 Correct 345 ms 164392 KB Output is correct
19 Correct 239 ms 157264 KB Output is correct
20 Correct 188 ms 156964 KB Output is correct
21 Correct 244 ms 157124 KB Output is correct
22 Correct 197 ms 159920 KB Output is correct
23 Correct 232 ms 160080 KB Output is correct
24 Correct 272 ms 159952 KB Output is correct
25 Correct 226 ms 160336 KB Output is correct
26 Correct 294 ms 160192 KB Output is correct
27 Correct 242 ms 147404 KB Output is correct
28 Correct 226 ms 155788 KB Output is correct
29 Correct 214 ms 149964 KB Output is correct
30 Correct 225 ms 150300 KB Output is correct
31 Correct 194 ms 151368 KB Output is correct
32 Correct 171 ms 146928 KB Output is correct
33 Correct 180 ms 149704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 99152 KB Output is correct
2 Correct 47 ms 99068 KB Output is correct
3 Correct 40 ms 99152 KB Output is correct
4 Correct 54 ms 99664 KB Output is correct
5 Correct 66 ms 100048 KB Output is correct
6 Correct 52 ms 99340 KB Output is correct
7 Correct 41 ms 99376 KB Output is correct
8 Correct 51 ms 99408 KB Output is correct
9 Correct 123 ms 107020 KB Output is correct
10 Correct 505 ms 265044 KB Output is correct
11 Correct 43 ms 99408 KB Output is correct
12 Correct 87 ms 100176 KB Output is correct
13 Correct 506 ms 268116 KB Output is correct
14 Correct 378 ms 268180 KB Output is correct
15 Correct 538 ms 269748 KB Output is correct
16 Correct 786 ms 277592 KB Output is correct
17 Correct 480 ms 270724 KB Output is correct
18 Correct 412 ms 266916 KB Output is correct
19 Correct 444 ms 270724 KB Output is correct
20 Correct 349 ms 270852 KB Output is correct
21 Correct 394 ms 270672 KB Output is correct
22 Correct 300 ms 266068 KB Output is correct
23 Correct 38 ms 99284 KB Output is correct
24 Correct 39 ms 99156 KB Output is correct
25 Correct 45 ms 99416 KB Output is correct
26 Correct 41 ms 99412 KB Output is correct
27 Correct 42 ms 99420 KB Output is correct
28 Correct 41 ms 99412 KB Output is correct
29 Correct 42 ms 99412 KB Output is correct
30 Correct 40 ms 99420 KB Output is correct
31 Correct 50 ms 99400 KB Output is correct
32 Correct 39 ms 99416 KB Output is correct
33 Correct 39 ms 99412 KB Output is correct
34 Correct 38 ms 99420 KB Output is correct
35 Correct 36 ms 99420 KB Output is correct
36 Correct 39 ms 99072 KB Output is correct
37 Correct 44 ms 99152 KB Output is correct
38 Correct 42 ms 99408 KB Output is correct
39 Correct 40 ms 99412 KB Output is correct
40 Correct 41 ms 99364 KB Output is correct
41 Correct 38 ms 99160 KB Output is correct
42 Correct 41 ms 99416 KB Output is correct
43 Correct 40 ms 99408 KB Output is correct
44 Correct 40 ms 99420 KB Output is correct
45 Correct 40 ms 99152 KB Output is correct
46 Correct 45 ms 99152 KB Output is correct
47 Correct 39 ms 99404 KB Output is correct
48 Correct 37 ms 99416 KB Output is correct
49 Correct 48 ms 99260 KB Output is correct
50 Correct 37 ms 99292 KB Output is correct
51 Correct 49 ms 99484 KB Output is correct
52 Correct 42 ms 99416 KB Output is correct
53 Correct 38 ms 99560 KB Output is correct
54 Correct 39 ms 99412 KB Output is correct
55 Correct 47 ms 99404 KB Output is correct
56 Correct 40 ms 99416 KB Output is correct
57 Correct 41 ms 99364 KB Output is correct
58 Correct 44 ms 99496 KB Output is correct
59 Correct 48 ms 99412 KB Output is correct
60 Correct 92 ms 107092 KB Output is correct
61 Correct 464 ms 265044 KB Output is correct
62 Correct 85 ms 107344 KB Output is correct
63 Correct 76 ms 103748 KB Output is correct
64 Correct 64 ms 105300 KB Output is correct
65 Correct 104 ms 107344 KB Output is correct
66 Correct 45 ms 99872 KB Output is correct
67 Correct 178 ms 176496 KB Output is correct
68 Correct 228 ms 176972 KB Output is correct
69 Correct 522 ms 251952 KB Output is correct
70 Correct 515 ms 252332 KB Output is correct
71 Correct 248 ms 213840 KB Output is correct
72 Correct 302 ms 213716 KB Output is correct
73 Correct 367 ms 239700 KB Output is correct
74 Correct 297 ms 240008 KB Output is correct
75 Correct 61 ms 104528 KB Output is correct
76 Correct 279 ms 168528 KB Output is correct
77 Correct 43 ms 99164 KB Output is correct
78 Correct 41 ms 99152 KB Output is correct
79 Correct 39 ms 99152 KB Output is correct
80 Correct 41 ms 99152 KB Output is correct
81 Correct 46 ms 99412 KB Output is correct
82 Correct 42 ms 99408 KB Output is correct
83 Correct 42 ms 99412 KB Output is correct
84 Correct 40 ms 99324 KB Output is correct
85 Correct 40 ms 99152 KB Output is correct
86 Correct 48 ms 99220 KB Output is correct
87 Correct 40 ms 99328 KB Output is correct
88 Correct 43 ms 99420 KB Output is correct
89 Correct 60 ms 99924 KB Output is correct
90 Correct 73 ms 100180 KB Output is correct
91 Correct 76 ms 99924 KB Output is correct
92 Correct 168 ms 156240 KB Output is correct
93 Correct 247 ms 160084 KB Output is correct
94 Correct 345 ms 164392 KB Output is correct
95 Correct 239 ms 157264 KB Output is correct
96 Correct 188 ms 156964 KB Output is correct
97 Correct 244 ms 157124 KB Output is correct
98 Correct 197 ms 159920 KB Output is correct
99 Correct 232 ms 160080 KB Output is correct
100 Correct 272 ms 159952 KB Output is correct
101 Correct 226 ms 160336 KB Output is correct
102 Correct 294 ms 160192 KB Output is correct
103 Correct 242 ms 147404 KB Output is correct
104 Correct 226 ms 155788 KB Output is correct
105 Correct 214 ms 149964 KB Output is correct
106 Correct 225 ms 150300 KB Output is correct
107 Correct 194 ms 151368 KB Output is correct
108 Correct 171 ms 146928 KB Output is correct
109 Correct 180 ms 149704 KB Output is correct
110 Correct 76 ms 100264 KB Output is correct
111 Correct 60 ms 99960 KB Output is correct
112 Correct 531 ms 262224 KB Output is correct
113 Correct 757 ms 255228 KB Output is correct
114 Correct 352 ms 202700 KB Output is correct
115 Correct 105 ms 138696 KB Output is correct
116 Correct 381 ms 166156 KB Output is correct
117 Correct 378 ms 174672 KB Output is correct
118 Correct 286 ms 165164 KB Output is correct
119 Correct 222 ms 165212 KB Output is correct
120 Correct 59 ms 105248 KB Output is correct
121 Correct 535 ms 178260 KB Output is correct
122 Correct 454 ms 174928 KB Output is correct
123 Correct 811 ms 259920 KB Output is correct
124 Correct 386 ms 259664 KB Output is correct
125 Correct 715 ms 260144 KB Output is correct
126 Correct 840 ms 273992 KB Output is correct
127 Correct 437 ms 258308 KB Output is correct
128 Correct 405 ms 258388 KB Output is correct
129 Correct 856 ms 268876 KB Output is correct
130 Correct 483 ms 258392 KB Output is correct