Submission #955761

#TimeUsernameProblemLanguageResultExecution timeMemory
955761aymanrsJail (JOI22_jail)C++14
0 / 100
8 ms460 KiB
#include<bits/stdc++.h> using namespace std; bool f; int T; void dfs(int n, vector<int> g[], int p, int d[], int pa[]){ pa[n] = p; d[n] = d[p]+1; for(int c : g[n]){ if(c == p) continue; dfs(c, g, n, d, pa); } } struct node { vector<node*> l; bool v = false, c = false; }; bool ans; void dfs(node* n){ n->v = n->c = true; for(node* c : n->l){ if(!ans) break; if(c->c){ ans = false; break; } if(!c->v) dfs(c); } n->c = false; } void solve(){ bool s1 = true; int n,m;cin >> n; vector<int> g[n+1]; for(int i = 1;i < n;i++){ int u, v;cin >> u >> v; s1 &= (u == i) && (v == i+1); g[u].push_back(v); g[v].push_back(u); } if(s1){ cin >> m; set<int> fore, bace; pair<int, int> ra[m]; bool ok = true; for(int i = 0;i < m;i++){ cin >> ra[i].first >> ra[i].second; if(ra[i].first < ra[i].second) { fore.insert(ra[i].second); auto p = bace.lower_bound(ra[i].first); if(p != bace.end() && *p <= ra[i].second) ok = false; } else { bace.insert(ra[i].second); swap(ra[i].first, ra[i].second); auto p = fore.lower_bound(ra[i].first); if(p != fore.end() && *p <= ra[i].second) ok = false; } } if(!ok){ cout << "No\n"; return; } sort(ra, ra+m, [](const auto& a, const auto& b){ if(a.first != b.first) return a.first < b.first; return a.second > b.second; }); int R = -1; for(auto p : ra){ if(R >= p.second){ cout << "No\n"; return; } R = max(R, p.second); } cout << "Yes\n"; return; } int d[n+1], p[n+1];d[0] = 0; dfs(1, g, 0, d, p); cin >> m; int s[m], t[m]; int sn[n+1], tn[n+1]; fill(sn+1, sn+n+1, -1);fill(tn+1, tn+n+1, -1); for(int i = 0;i < m;i++){ cin >> s[i] >> t[i]; sn[s[i]] = i; tn[t[i]] = i; } node mg[m]; for(int i = 0;i < m;i++){ vector<int> path; if(d[s[i]] < d[t[i]]) swap(s[i], t[i]); while(d[s[i]] > d[t[i]]){ path.push_back(s[i]); s[i] = p[s[i]]; } while(true){ path.push_back(s[i]); if(s[i] == t[i]) break; path.push_back(t[i]); s[i] = p[s[i]];t[i] = p[t[i]]; } for(int _ : path){ if(sn[_] != -1 && sn[_] != i) mg[i].l.push_back(&mg[sn[_]]); if(tn[_] != -1 && tn[_] != i) mg[tn[_]].l.push_back(&mg[i]); } } ans = true; for(int i = 0;i < m && ans;i++){ if(!mg[i].v) dfs(&mg[i]); } if(ans)cout << "Yes\n"; else cout << "No\n"; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int t;cin >> t; while(t--) 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...