Submission #1040129

#TimeUsernameProblemLanguageResultExecution timeMemory
1040129SharkyJail (JOI22_jail)C++17
77 / 100
5076 ms444672 KiB
#include <bits/stdc++.h> using namespace std; const int N = 120008; const int LG = 19; int n, m, sz[N], dep[N], node = 0, pr[N][LG], rt[N], tin[N], pa[N], timer = 0, leaf[N * 3], ls[N * 20], rs[N * 20], st[2], vis[N * 20], cyc = 0, col[N * 20]; vector<int> adj[N], g[N * 20]; void dfs(int u) { sz[u] = 1; for (int i = 1; i < LG; i++) pr[u][i] = pr[pr[u][i - 1]][i - 1]; for (auto& v : adj[u]) { if (v == pr[u][0]) continue; pr[v][0] = u; dep[v] = dep[u] + 1; pa[v] = u; dfs(v); if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]); sz[u] += sz[v]; } } void dfs_hld(int u) { tin[u] = ++timer; for (int v : adj[u]) { if (v == pr[u][0]) continue; rt[v] = (v == adj[u][0] ? rt[u] : v); dfs_hld(v); } } int jump(int s, int di) { for (int i = LG - 1; i >= 0; i--) if (di & (1 << i)) s = pr[s][i]; return s; } int lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); v = jump(v, dep[v] - dep[u]); if (u == v) return u; for (int i = LG - 1; i >= 0; i--) { int ut = pr[u][i], vt = pr[v][i]; if (ut != vt) u = ut, v = vt; } return pr[u][0]; } void build(int l, int r, int id, bool up) { if (l == r) { leaf[l] = id; return; } int mid = (l + r) / 2; if (l != mid || !up) { ls[id] = ++node; build(l, mid, ls[id], up); } else ls[id] = leaf[l]; if (mid + 1 != r || !up) { rs[id] = ++node; build(mid + 1, r, rs[id], up); } else rs[id] = leaf[r]; if (!up) { g[id].push_back(ls[id]); g[id].push_back(rs[id]); } else { g[ls[id]].push_back(id); g[rs[id]].push_back(id); } } void update(int ql, int qr, int v, int l, int r, int id, bool up) { if (qr < l || r < ql) return; if (ql <= l && r <= qr) { if (!up) { // cout << "edge: " << v << "-->" << '[' << l << ", " << r << ']' << "\n"; g[leaf[v]].push_back(id); } else { // cout << "edge: " << '[' << l << ", " << r << ']' << " --> " << v << "\n"; g[id].push_back(leaf[v]); } return; } int mid = (l + r) / 2; update(ql, qr, v, l, mid, ls[id], up); update(ql, qr, v, mid + 1, r, rs[id], up); } void pti(int x, int l, int r) { update(l, r, x, 1, 2 * n + m, st[0], 0); } void itp(int x, int l, int r) { update(l, r, x, 1, 2 * n + m, st[1], 1); } void path(int x, int u, int v, bool b) { // cout << x << " " << u << " " << v << " " << b << "\n"; for (; rt[u] != rt[v]; v = pa[rt[v]]) { if (dep[rt[u]] > dep[rt[v]]) swap(u, v); if (!b) pti(x + 2 * n, tin[rt[v]] + n, tin[v] + n); else itp(x + 2 * n, tin[rt[v]], tin[v]); } if (dep[u] > dep[v]) swap(u, v); if (!b) pti(x + 2 * n, tin[u] + n, tin[v] + n); else itp(x + 2 * n, tin[u], tin[v]); } bool DFS(int u) { col[u] = 1; for (int v : g[u]) { if (!col[v]) { if (DFS(v)) { cyc = 1; return true; } } else if (col[v] == 1) { cyc = 1; return true; } } col[u] = 2; return false; } void solve() { cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } cin >> m; rt[1] = pr[1][0] = 1; dfs(1); dfs_hld(1); st[0] = ++node; build(1, 2 * n + m, node, 0); st[1] = ++node; build(1, 2 * n + m, node, 1); for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; int x = lca(u, v); g[leaf[n * 2 + i]].push_back(leaf[tin[u]]); g[leaf[tin[v] + n]].push_back(leaf[n * 2 + i]); // cout << "edge: " << n * 2 + i << " --> " << tin[u] << '\n'; // cout << "edge: " << tin[v] + n << " --> " << n * 2 + i << '\n'; int d = max(dep[u], dep[v]) - dep[x] - 1; if (u != x) { path(i, pa[u], v, 1); } else { path(i, jump(v, d), v, 1); } if (v != x) { path(i, u, pa[v], 0); } else { path(i, u, jump(u, d), 0); } } // for (int i = 1; i <= node; i++) for (int j : g[i]) cout << i << " --> " << j << '\n'; for (int i = 1; i <= node; i++) if (!col[i] && DFS(i)) break; cout << (cyc ? "No" : "Yes") << '\n'; for (int i = 1; i <= n; i++) { sz[i] = dep[i] = rt[i] = tin[i] = pa[i] = 0; adj[i].clear(); for (int j = 0; j < LG; j++) pr[i][j] = 0; } for (int i = 1; i <= n * 3; i++) leaf[i] = 0; for (int i = 1; i <= node; i++) { g[i].clear(); ls[i] = rs[i] = col[i] = vis[i] = 0; } timer = cyc = node = st[0] = st[1] = 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); int q; cin >> q; while (q--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...