Submission #876765

#TimeUsernameProblemLanguageResultExecution timeMemory
876765serkanrashidJail (JOI22_jail)C++14
61 / 100
5051 ms30020 KiB
#include <bits/stdc++.h> #define endl "\n" using namespace std; const int maxn = 12*1e4+5; int q,n,m; vector<int>g[maxn],v[maxn]; int s[maxn],t[maxn],used[maxn]; int jump[maxn][21],lvl[maxn]; bool ans; void precom() { for(int i=1;i<=n;i++) { g[i].clear(); used[i] = 0; v[i].clear(); } } void read() { cin >> n; precom(); int a,b; for(int i=1;i<n;i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } cin >> m; for(int i=1;i<=m;i++) { cin >> s[i] >> t[i]; } } void dfs_make(int beg, int par) { jump[beg][0] = par; lvl[beg] = lvl[par] + 1; for(int nb:g[beg]) if(nb!=par) dfs_make(nb,beg); } void make_lca() { lvl[1] = 0; dfs_make(1,1); for(int j=1;j<=20;j++) { for(int i=1;i<=n;i++) { jump[i][j] = jump[jump[i][j-1]][j-1]; } } } int get_lca(int a, int b) { int raz = abs(lvl[a]-lvl[b]); for(int j=20;j>=0;j--) { if(raz&(1<<j)) { if(lvl[a]>lvl[b]) a = jump[a][j]; else b = jump[b][j]; } } if(a==b) return a; for(int j=20;j>=0;j--) { if(jump[a][j]!=jump[b][j]) { a = jump[a][j]; b = jump[b][j]; } } return jump[a][0]; } bool check(int a, int b, int c) { int lca = get_lca(a,b); if(get_lca(c,lca)!=lca) return false; return (get_lca(c,a)==c || get_lca(c,b) == c); } void dfs(int beg) { used[beg] = 1; for(int nb:v[beg]) { if(used[nb]==1) ans = false; if(!used[nb]) dfs(nb); } used[beg] = 2; } void solve() { make_lca(); for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++) { if(i==j) continue; int chis = 0; if(check(s[i],t[i],s[j])) chis += 2; if(check(s[i],t[i],t[j])) chis += 1; if((chis&2)) v[j].push_back(i); if((chis&1)) v[i].push_back(j); } } ans = true; for(int i=1;i<=m;i++) used[i] = 0; for(int i=1;i<=m;i++) { if(!used[i]) dfs(i); } if(ans) cout << "Yes" << endl; else cout << "No" << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> q; for(int i=1;i<=q;i++) { read(); solve(); } return 0; } /* 1 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 4 1 5 2 6 3 7 4 8 */
#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...