Submission #994297

#TimeUsernameProblemLanguageResultExecution timeMemory
994297gortomiJail (JOI22_jail)C++17
60 / 100
434 ms1048576 KiB
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g, g2; vector<int> vis; vector<int> path; vector<vector<int> > p; void dfs(int v, int pa) { path.push_back(v); p[v] = path; for(auto x : g[v]) { if(x != pa) dfs(x, v); } path.pop_back(); } bool c(int v) { vis[v] = 1; for(auto x : g2[v]) { //cout << v << " " << x << endl; if(vis[x] == 1) return 1; if(vis[x] == 0) if(c(x)) return 1; } vis[v] = 2; return 0; } void solve() { int n, m; cin >> n; g.assign(n + 1, {}); vis.assign(n + 1, 0); p.assign(n + 1, {}); for(int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } cin >> m; g2.assign(m + 1, {}); vector<int> a(m), b(m); for(int i = 0; i < m; i++) cin >> a[i] >> b[i]; dfs(1, 0); //return; vector<vector<int> > inc(n + 1); for(int i = 0; i < m; i++) { vector<int> fi = p[a[i]], se = p[b[i]]; int y = fi.size(), z = se.size(); int be = 0; while(be < y - 1 && be < z - 1) { if(fi[be + 1] == se[be + 1]) be++; else break; } for(int j = be; j < y; j++) { inc[fi[j]].push_back(i); } for(int j = be; j < z; j++) { inc[se[j]].push_back(i); } } for(int i = 0; i < m; i++) { for(auto x : inc[a[i]]) { if(x != i) g2[x].push_back(i); } for(auto x : inc[b[i]]) { if(x != i) g2[i].push_back(x); } } vis.assign(m + 1, 0); for(int i = 0; i < m; i++) { if(vis[i] == 0 && c(i)) { cout << "No\n"; return; } } cout << "Yes\n"; } 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...