Submission #1039078

# Submission time Handle Problem Language Result Execution time Memory
1039078 2024-07-30T12:34:19 Z juicy Jail (JOI22_jail) C++17
0 / 100
43 ms 100184 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();
    }
    while (tot > 0) {
      gph[tot].clear();
      vis[tot] = 0;
      --tot;
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 38 ms 99152 KB Output is correct
2 Incorrect 38 ms 99164 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 99160 KB Output is correct
2 Correct 39 ms 99152 KB Output is correct
3 Incorrect 43 ms 100184 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 99160 KB Output is correct
2 Correct 39 ms 99152 KB Output is correct
3 Incorrect 43 ms 100184 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 99160 KB Output is correct
2 Correct 39 ms 99152 KB Output is correct
3 Incorrect 43 ms 100184 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 99160 KB Output is correct
2 Correct 39 ms 99152 KB Output is correct
3 Incorrect 43 ms 100184 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 41 ms 99156 KB Output is correct
2 Correct 39 ms 99164 KB Output is correct
3 Incorrect 41 ms 99156 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 99152 KB Output is correct
2 Incorrect 38 ms 99164 KB Output isn't correct
3 Halted 0 ms 0 KB -