Submission #950882

#TimeUsernameProblemLanguageResultExecution timeMemory
950882vladiliusJail (JOI22_jail)C++17
0 / 100
1 ms348 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin>>q; while (q--){ int n; cin>>n; vector<int> g[n + 1]; for (int i = 1; i < n; i++){ int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } int m; cin>>m; vector<int> s(m + 1), t(m + 1); for (int i = 1; i <= m; i++){ cin>>s[i]>>t[i]; } vector<int> tin(n + 1), tout(n + 1), pw[n + 1], pp(n + 1); int lg = ceil(log2(n)) + 2; for (int i = 1; i <= n; i++){ pw[i].resize(lg); } int timer = 0; function<void(int, int)> fill = [&](int v, int pr){ tin[v] = ++timer; pw[v][0] = pp[v] = pr; for (int i = 1; i < lg; i++){ pw[v][i] = pw[pw[v][i - 1]][i - 1]; } for (int i: g[v]){ if (i == pr) continue; fill(i, v); } tout[v] = timer; }; fill(1, 1); pp[1] = 0; auto check = [&](int x, int y){ return (tin[x] <= tin[y] && tout[x] >= tout[y]); }; auto lca = [&](int x, int y){ if (check(x, y)) return x; if (check(y, x)) return y; for (int i = lg - 1; i >= 0; i--){ if (!check(pw[x][i], y)){ x = pw[x][i]; } } return pw[x][0]; }; vector<int> l(m + 1), d[n + 1]; for (int i = 1; i <= m; i++){ l[i] = lca(s[i], t[i]); d[s[i]].push_back(i); } vector<bool> vis(m + 1); vector<int> G[m + 1]; function<void(int)> dfs = [&](int v){ if (vis[v] || !v) return; vis[v] = true; int x = s[v]; set<int> all; while (check(l[v], x)){ for (int i: d[x]){ all.insert(i); } x = pp[x]; } x = t[v]; while (x != l[v]){ for (int i: d[x]){ all.insert(i); } x = pp[x]; } // all.erase(all.find(v)); for (int i: all){ if (i == v) continue; G[v].push_back(i); dfs(i); } d[s[v]].erase(find(d[s[v]].begin(), d[s[v]].end(), v)); d[t[v]].push_back(v); }; for (int i = 1; i <= m; i++) dfs(i); vis.assign(m + 1, false); bool out = true; function<void(int)> find = [&](int v){ if (vis[v]){ out = false; return; } vis[v] = true; for (int i: G[v]){ find(i); } }; vector<bool> used(m + 1); for (int i = 1; i <= m; i++){ for (int j: G[i]){ used[j] = true; } } for (int i = 1; i <= m; i++){ if (used[i]) continue; find(i); if (!out) break; } for (int i = 1; i <= m; i++){ if (!vis[i]){ out = false; break; } } cout<<(out ? "Yes" : "No")<<"\n"; } }
#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...