1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 1000006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m , q; vi G[MAXN]; int dfn[MAXN] , low[MAXN] , g[MAXN][19] , clo , stk[MAXN] , top , ins[MAXN] , dep[MAXN] , odd[MAXN]; int od[MAXN] , bel[MAXN]; void tarjan( int u , int f ) { bel[u] = bel[f]; dfn[u] = low[u] = ++ clo , ins[u] = 1; stk[++ top] = u; for( int v : G[u] ) { if( v == f ) continue; if( !dfn[v] ) { g[v][0] = u; rep( k , 1 , 18 ) if( g[g[v][k-1]][k-1] ) g[v][k] = g[g[v][k-1]][k-1]; else break; dep[v] = dep[u] + 1; tarjan( v , u ) , low[u] = min( low[u] , low[v] ); } else if( ins[v] ) low[u] = min( low[u] , dfn[v] ) , odd[u] |= ( ~( dep[v] + dep[u] ) & 1 ); } if( dfn[u] == low[u] ) { int flg = 0 , t = top; while( stk[t] != u && !flg ) flg = odd[stk[t]] , -- t; while( stk[top] != u ) { od[stk[top]] |= flg; ins[stk[top]] = 0; -- top; } -- top , ins[u] = 0; } } inline int lca( int u , int v ) { if( dep[u] < dep[v] ) u ^= v ^= u ^= v; if( dep[u] != dep[v] ) per( k , 18 , 0 ) if( dep[g[u][k]] >= dep[v] ) u = g[u][k]; if( u == v ) return u; per( k , 18 , 0 ) if( g[u][k] != g[v][k] ) u = g[u][k] , v = g[v][k]; return g[u][0]; } int S[MAXN]; void dfs( int u , int f ) { S[u] += od[u]; for( int v : G[u] ) if( v != f && dep[v] == dep[u] + 1 ) S[v] += S[u] , dfs( v , u ); } inline bool fuck( int u , int v ) { if( bel[u] != bel[v] || u == v ) return false; if( dep[u] + dep[v] & 1 ) return true; return S[u] + S[v] - 2 * S[lca( u , v )] > 0; } void solve() { cin >> n >> m; int u , v; rep( i , 1 , m ) { scanf("%d%d",&u,&v); G[u].pb( v ) , G[v].pb( u ); } rep( i , 1 , n ) if( !dfn[i] ) bel[i] = i , dep[i] = 1 , tarjan( i , i ) , dfs( i , i ); cin >> q; rep( i , 1 , q ) scanf("%d%d",&u,&v) , puts( fuck( u , v ) ? "Yes" : "No" ); } signed main() {
solve(); }
|