Submission #701008

# Submission time Handle Problem Language Result Execution time Memory
701008 2023-02-19T17:34:24 Z dongliu0426 Jail (JOI22_jail) C++17
26 / 100
412 ms 70364 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
 
namespace x = __gnu_pbds;
template <typename T>
using ordered_set = x::tree<T, x::null_type, less<T>, x::rb_tree_tag, x::tree_order_statistics_node_update>;
 
template <typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
 
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
#define x first
#define y second
using ll = long long;
using ld = long double;
 
const int N = 1.25e5, M = 5 * N;
 
vector<int> g[N], g2[M];
 
char used[M];
 
bool dfs_cycle(int u) {
  if (used[u] == 1) {
    return false;
  }
  used[u] = 1;
  for (int v : g2[u]) {
    if (used[v] != 2 && !dfs_cycle(v)) {
      return false;
    }
  }
  used[u] = 2;
  return true;
}
 
int sz[N];
 
int dfs_sz(int u = 0, int p = -1) {
  int res = 1;
  for (int v : g[u]) {
    if (v != p) {
      res += dfs_sz(v, u);
    }
  }
  return sz[u] = res;
}
 
int timer, order[N], lnk[N], tout[N], parent[N];
 
void dfs_hld(int u = 0, int up = 0, int p = -1) {
  order[u] = timer;
  parent[timer] = p == -1 ? -1 : order[p];
  lnk[timer] = order[up];
  ++timer;
  sort(all(g[u]), [](int x, int y) {
    return sz[x] > sz[y];
  });
  bool is_first = true;
  for (int v : g[u]) {
    if (v != p) {
      dfs_hld(v, is_first ? up : v, u);
      is_first = false;
    }
  }
  tout[order[u]] = timer;
}
 
bool in(int x0, int y0, int x1, int y1) {
  return x0 <= x1 && y1 <= y0;
}
 
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  int testcases;
  cin >> testcases;
  while (testcases--) {
    int n, m;
    cin >> n;
    for (int i = 0; i < 5 * n; ++i) {
      g2[i].clear();
    }
    for (int i = 0; i < n; ++i) {
      g[i].clear();
    }
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      g[--u].push_back(--v);
      g[v].push_back(u);
    }
    timer = 0;
    dfs_sz();
    dfs_hld();
    assert(timer == n);
 
    cin >> m;
 
 
    for (int i = 2 * n - 1; i > 0; --i) {
      g2[i / 2].push_back(i);
      g2[2 * n + i].push_back(2 * n + i / 2);
    }
 
    for (int person = 0; person < m; ++person) {
      int u = 4 * n + person;
 
      int s, t;
      cin >> s >> t;
      s = order[s - 1], t = order[t - 1];
 
      auto use_cut = [&](int l, int r) {
        int x = n + l + (l == s ? 1 : 0), y = n + r - (r == s ? 1 : 0);
        while (x <= y) {
          if (x & 1)  g2[u].push_back(x);
          if (!(y & 1)) g2[u].push_back(y);
          x = (x + 1) / 2;
          y = (y - 1) / 2;
        }
        // for (int i = l; i <= r; ++i) {
        //  if (i != s) {
        //    g2[u].push_back(n + i);
        //  }
        // }
 
        x = n + l + (l == t ? 1 : 0), y = n + r - (r == t ? 1 : 0);
        while (x <= y) {
          if (x & 1)  g2[2 * n + x].push_back(u);
          if (!(y & 1)) g2[2 * n + y].push_back(u);
          x = (x + 1) / 2;
          y = (y - 1) / 2;
        }
        // for (int i = l; i <= r; ++i) {
        //  if (i != t) {
        //    g2[3 * n + i].push_back(u);
        //  }
        // }
      };
 
      int a = s, b = t;
      while (lnk[a] != lnk[b]) {
        if (order[lnk[a]] > order[lnk[b]]) {
          use_cut(lnk[a], a);
          a = parent[lnk[a]];
        } else {
          // assert(!in(lnk[b], tout[lnk[b]], a, tout[a]));
          use_cut(lnk[b], b);
          b = parent[lnk[b]];
        }
      }
      use_cut(min(a, b), max(a, b));
      g2[n + s].push_back(u);
      g2[u].push_back(3 * n + t);
    }
    fill(used, used + 5 * n, 0);
    bool flag = true;
    for (int i = 0; i < 5 * n; ++i) {
      if (!used[i]) {
        flag &= dfs_cycle(i);
      }
    }
    cout << (flag ? "Yes\n" : "No\n");
  }
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 18004 KB Output is correct
2 Correct 10 ms 17876 KB Output is correct
3 Correct 9 ms 17876 KB Output is correct
4 Correct 21 ms 17992 KB Output is correct
5 Correct 38 ms 17992 KB Output is correct
6 Correct 11 ms 18020 KB Output is correct
7 Correct 10 ms 18004 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 43 ms 19540 KB Output is correct
10 Correct 68 ms 47256 KB Output is correct
11 Correct 16 ms 17876 KB Output is correct
12 Correct 43 ms 17992 KB Output is correct
13 Correct 123 ms 52044 KB Output is correct
14 Correct 142 ms 52300 KB Output is correct
15 Correct 240 ms 56632 KB Output is correct
16 Correct 412 ms 70364 KB Output is correct
17 Correct 156 ms 60864 KB Output is correct
18 Correct 142 ms 54628 KB Output is correct
19 Correct 149 ms 59496 KB Output is correct
20 Correct 169 ms 59524 KB Output is correct
21 Correct 177 ms 59780 KB Output is correct
22 Correct 116 ms 50856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17876 KB Output is correct
2 Correct 9 ms 17960 KB Output is correct
3 Correct 10 ms 18004 KB Output is correct
4 Correct 10 ms 18004 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18004 KB Output is correct
7 Correct 11 ms 18004 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 11 ms 18004 KB Output is correct
10 Correct 11 ms 17876 KB Output is correct
11 Correct 11 ms 17916 KB Output is correct
12 Correct 13 ms 17876 KB Output is correct
13 Correct 12 ms 17912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17876 KB Output is correct
2 Correct 9 ms 17960 KB Output is correct
3 Correct 10 ms 18004 KB Output is correct
4 Correct 10 ms 18004 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18004 KB Output is correct
7 Correct 11 ms 18004 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 11 ms 18004 KB Output is correct
10 Correct 11 ms 17876 KB Output is correct
11 Correct 11 ms 17916 KB Output is correct
12 Correct 13 ms 17876 KB Output is correct
13 Correct 12 ms 17912 KB Output is correct
14 Correct 11 ms 17960 KB Output is correct
15 Correct 9 ms 17876 KB Output is correct
16 Correct 10 ms 18004 KB Output is correct
17 Correct 10 ms 18000 KB Output is correct
18 Correct 12 ms 18004 KB Output is correct
19 Correct 9 ms 17968 KB Output is correct
20 Correct 13 ms 18004 KB Output is correct
21 Correct 11 ms 18004 KB Output is correct
22 Correct 11 ms 18004 KB Output is correct
23 Correct 9 ms 17876 KB Output is correct
24 Correct 9 ms 17876 KB Output is correct
25 Correct 11 ms 18004 KB Output is correct
26 Correct 10 ms 17952 KB Output is correct
27 Correct 12 ms 18004 KB Output is correct
28 Correct 12 ms 18056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17876 KB Output is correct
2 Correct 9 ms 17960 KB Output is correct
3 Correct 10 ms 18004 KB Output is correct
4 Correct 10 ms 18004 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18004 KB Output is correct
7 Correct 11 ms 18004 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 11 ms 18004 KB Output is correct
10 Correct 11 ms 17876 KB Output is correct
11 Correct 11 ms 17916 KB Output is correct
12 Correct 13 ms 17876 KB Output is correct
13 Correct 12 ms 17912 KB Output is correct
14 Correct 11 ms 17960 KB Output is correct
15 Correct 9 ms 17876 KB Output is correct
16 Correct 10 ms 18004 KB Output is correct
17 Correct 10 ms 18000 KB Output is correct
18 Correct 12 ms 18004 KB Output is correct
19 Correct 9 ms 17968 KB Output is correct
20 Correct 13 ms 18004 KB Output is correct
21 Correct 11 ms 18004 KB Output is correct
22 Correct 11 ms 18004 KB Output is correct
23 Correct 9 ms 17876 KB Output is correct
24 Correct 9 ms 17876 KB Output is correct
25 Correct 11 ms 18004 KB Output is correct
26 Correct 10 ms 17952 KB Output is correct
27 Correct 12 ms 18004 KB Output is correct
28 Correct 12 ms 18056 KB Output is correct
29 Correct 11 ms 17920 KB Output is correct
30 Correct 12 ms 18000 KB Output is correct
31 Correct 11 ms 18004 KB Output is correct
32 Correct 12 ms 18004 KB Output is correct
33 Incorrect 10 ms 17932 KB Output isn't correct
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17876 KB Output is correct
2 Correct 9 ms 17960 KB Output is correct
3 Correct 10 ms 18004 KB Output is correct
4 Correct 10 ms 18004 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18004 KB Output is correct
7 Correct 11 ms 18004 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 11 ms 18004 KB Output is correct
10 Correct 11 ms 17876 KB Output is correct
11 Correct 11 ms 17916 KB Output is correct
12 Correct 13 ms 17876 KB Output is correct
13 Correct 12 ms 17912 KB Output is correct
14 Correct 11 ms 17960 KB Output is correct
15 Correct 9 ms 17876 KB Output is correct
16 Correct 10 ms 18004 KB Output is correct
17 Correct 10 ms 18000 KB Output is correct
18 Correct 12 ms 18004 KB Output is correct
19 Correct 9 ms 17968 KB Output is correct
20 Correct 13 ms 18004 KB Output is correct
21 Correct 11 ms 18004 KB Output is correct
22 Correct 11 ms 18004 KB Output is correct
23 Correct 9 ms 17876 KB Output is correct
24 Correct 9 ms 17876 KB Output is correct
25 Correct 11 ms 18004 KB Output is correct
26 Correct 10 ms 17952 KB Output is correct
27 Correct 12 ms 18004 KB Output is correct
28 Correct 12 ms 18056 KB Output is correct
29 Correct 11 ms 17920 KB Output is correct
30 Correct 12 ms 18000 KB Output is correct
31 Correct 11 ms 18004 KB Output is correct
32 Correct 12 ms 18004 KB Output is correct
33 Incorrect 10 ms 17932 KB Output isn't correct
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 17884 KB Output is correct
2 Correct 9 ms 17876 KB Output is correct
3 Correct 8 ms 17876 KB Output is correct
4 Correct 9 ms 17876 KB Output is correct
5 Correct 18 ms 17876 KB Output is correct
6 Correct 10 ms 17968 KB Output is correct
7 Correct 11 ms 17936 KB Output is correct
8 Correct 10 ms 17888 KB Output is correct
9 Correct 9 ms 17876 KB Output is correct
10 Correct 9 ms 17876 KB Output is correct
11 Correct 9 ms 17876 KB Output is correct
12 Incorrect 12 ms 18004 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 18004 KB Output is correct
2 Correct 10 ms 17876 KB Output is correct
3 Correct 9 ms 17876 KB Output is correct
4 Correct 21 ms 17992 KB Output is correct
5 Correct 38 ms 17992 KB Output is correct
6 Correct 11 ms 18020 KB Output is correct
7 Correct 10 ms 18004 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 43 ms 19540 KB Output is correct
10 Correct 68 ms 47256 KB Output is correct
11 Correct 16 ms 17876 KB Output is correct
12 Correct 43 ms 17992 KB Output is correct
13 Correct 123 ms 52044 KB Output is correct
14 Correct 142 ms 52300 KB Output is correct
15 Correct 240 ms 56632 KB Output is correct
16 Correct 412 ms 70364 KB Output is correct
17 Correct 156 ms 60864 KB Output is correct
18 Correct 142 ms 54628 KB Output is correct
19 Correct 149 ms 59496 KB Output is correct
20 Correct 169 ms 59524 KB Output is correct
21 Correct 177 ms 59780 KB Output is correct
22 Correct 116 ms 50856 KB Output is correct
23 Correct 9 ms 17876 KB Output is correct
24 Correct 9 ms 17960 KB Output is correct
25 Correct 10 ms 18004 KB Output is correct
26 Correct 10 ms 18004 KB Output is correct
27 Correct 10 ms 17876 KB Output is correct
28 Correct 11 ms 18004 KB Output is correct
29 Correct 11 ms 18004 KB Output is correct
30 Correct 11 ms 18004 KB Output is correct
31 Correct 11 ms 18004 KB Output is correct
32 Correct 11 ms 17876 KB Output is correct
33 Correct 11 ms 17916 KB Output is correct
34 Correct 13 ms 17876 KB Output is correct
35 Correct 12 ms 17912 KB Output is correct
36 Correct 11 ms 17960 KB Output is correct
37 Correct 9 ms 17876 KB Output is correct
38 Correct 10 ms 18004 KB Output is correct
39 Correct 10 ms 18000 KB Output is correct
40 Correct 12 ms 18004 KB Output is correct
41 Correct 9 ms 17968 KB Output is correct
42 Correct 13 ms 18004 KB Output is correct
43 Correct 11 ms 18004 KB Output is correct
44 Correct 11 ms 18004 KB Output is correct
45 Correct 9 ms 17876 KB Output is correct
46 Correct 9 ms 17876 KB Output is correct
47 Correct 11 ms 18004 KB Output is correct
48 Correct 10 ms 17952 KB Output is correct
49 Correct 12 ms 18004 KB Output is correct
50 Correct 12 ms 18056 KB Output is correct
51 Correct 11 ms 17920 KB Output is correct
52 Correct 12 ms 18000 KB Output is correct
53 Correct 11 ms 18004 KB Output is correct
54 Correct 12 ms 18004 KB Output is correct
55 Incorrect 10 ms 17932 KB Output isn't correct
56 Halted 0 ms 0 KB -