Submission #631357

#TimeUsernameProblemLanguageResultExecution timeMemory
631357mdn2002Jail (JOI22_jail)C++14
0 / 100
32 ms9688 KiB
#include<bits/stdc++.h> using namespace std; int n , m , st [200005][21] , dp [200005] , s [200005] , t [200005] , vis [200005] , ff; vector < int > gr [200005] , grr [200005]; void dfs ( int x , int p ) { dp [x] = dp [p] + 1; st [x][0] = p; for ( auto u : gr [x] ) { if ( u == p ) continue; dfs ( u , x ); } } void dfss ( int x , int p ) { vis [x] = 1; for ( auto u : grr [x] ) { if ( u == p ) continue; if ( vis [u] == 1 ) { ff = 1; continue; } else if ( vis [u] == 2 ) continue; else dfss ( u , x ); } vis [x] = 2; } int lca ( int x , int y ) { if ( dp [x] > dp [y] ) swap ( x , y ); int dif = dp [y] - dp [x]; for ( int i = 0 ; i <= 20 ; i ++ ) { if ( ( dif & ( 1 << i ) ) ) y = st [y][i]; } if ( x == y ) return x; for ( int i = 20 ; i >= 0 ; i -- ) { if ( st [x][i] != st [y][i] ) { x = st [x][i]; y = st [y][i]; } } return st [x][0]; } int inbet ( int x , int y , int z ) { if ( dp [z] > dp [x] && dp [z] > dp [y] ) return 0; int aa = lca ( x , y ); if ( dp [aa] > dp [z] ) return 0; int a = lca ( x , z ); int b = lca ( y , z ); if ( a == z || b == z ) return 1; return 0; } int main() { int q; cin >> q; while ( q -- ) { ff = 0; cin >> n; for ( int i = 1 ; i <= n ; i ++ ) { gr [i] . clear (); grr [i] . clear (); dp [i] = 0; for ( int j = 0 ; j <= 20 ; j ++ ) st [i][j] = 0; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int x , y; cin >> x >> y; gr [x] . push_back ( y ); gr [y] . push_back ( x ); } dfs ( 1 , 0 ); for ( int i = 1 ; i <= 20 ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) st [j][i] = st [ st [j][ i - 1 ] ][ i - 1 ]; } int fff = 0; cin >> m; for ( int i = 1 ; i <= m ; i ++ ) cin >> s [i] >> t [i]; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( i == j ) continue; if ( inbet ( s [i] , t [i] , s [j] ) ) { if ( inbet ( s [i] , t [i] , t [j] ) ) fff = 1; grr [i] . push_back ( j ); } } } if ( fff ) { cout << "No" << endl; continue; } for ( int i = 1 ; i <= n ; i ++ ) { if ( vis [i] == 0 ) dfss ( i , 0 ); } if ( ff ) cout << "No" << endl; else cout << "Yes" << endl; } }
#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...