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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 300006
#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; const int P = 998244353; int n , q; vi G[MAXN];
int g[MAXN][20] , dep[MAXN]; pii S[MAXN]; void dfs( int u , int f ) { dep[u] = dep[f] + 1; for( int v : G[u] ) if( v != f ) { g[v][0] = u; rep( k , 1 , 17 ) if( g[g[v][k - 1]][k - 1] ) g[v][k] = g[g[v][k - 1]][k - 1]; else break; dfs( v , u ); } } int lca( int u , int v ) { if( dep[u] < dep[v] ) swap( u , v ); per( k , 17 , 0 ) if( dep[g[u][k]] >= dep[v] ) u = g[u][k]; if( u == v ) return u; per( k , 17 , 0 ) if( g[u][k] != g[v][k] ) u = g[u][k] , v = g[v][k]; return g[u][0]; } int jump( int u , int k ) { per( t , 17 , 0 ) if( k & ( 1 << t ) ) u = g[u][t]; return u; } int dis( int u , int v ) { return dep[u] + dep[v] - 2 * dep[lca( u , v )]; } void ck( pii& r , int& d , pii b ) { int p = dis( b.fi , b.se ); if( p > d ) d = p , r = b; } pii merge( pii a , pii b ) { pii re = a; int ds = dis( a.fi , a.se ); ck( re , ds , b ); ck( re , ds , mp( a.fi , b.fi ) ) , ck( re , ds , mp( a.se , b.fi ) ); ck( re , ds , mp( a.fi , b.se ) ) , ck( re , ds , mp( a.se , b.se ) ); return re; } void afs( int u , int f ) { S[u] = mp( u , u ); for( int v : G[u] ) if( v != f ) { afs( v , u ); S[u] = merge( S[u] , S[v] ); } } pii ot[MAXN]; pii pr , su[MAXN]; void pfs( int u , int f ) { for( int i = G[u].size() - 1 ; i >= 0 ; -- i ) { int v = G[u][i]; if( v == f ) { su[i] = su[i + 1]; continue; } if( i != G[u].size() - 1 ) su[i] = merge( S[v] , su[i + 1] ); else su[i] = S[v]; } pr = u == f ? mp( u , u ) : merge( mp( u , u ) , ot[u] ); rep( i , 0 , G[u].size() - 1 ) { int v = G[u][i]; if( v == f ) continue; ot[v] = i != G[u].size() - 1 ? merge( pr , su[i + 1] ) : pr; pr = merge( pr , S[v] ); } for( int v : G[u] ) if( v != f ) pfs( v , u ); }
void solve() { cin >> n >> q; rep( i , 2 , n ) { int u , v; scanf("%d%d",&u,&v); G[u].pb( v ) , G[v].pb( u ); } dfs( 1 , 1 ); afs( 1 , 1 ); pfs( 1 , 1 ); rep( i , 1 , q ) { int u , v , k; scanf("%d%d%d",&u,&v,&k); if( k & 1 ) swap( u , v ); int l = lca( u , v ) , t = jump( v , ( k + 1 ) / 2 ) , d = dep[u] + dep[v] - 2 * dep[l]; int ok = 0; if( ( k + 1 >> 1 ) >= d ) ok = 0; else if( !t || dep[t] <= dep[l] ) { int d = ( dep[u] - dep[l] ) - ( ( k + 1 >> 1 ) - ( dep[v] - dep[l] ) ); int t = jump( u , d - 1 ); int x = S[t].fi , y = S[t].se; if( dis( x , u ) >= k / 2 || dis( y , u ) >= k / 2 ) ok = 1; } else { int x = ot[t].fi , y = ot[t].se;
if( dis( x , u ) >= k / 2 || dis( y , u ) >= k / 2 ) ok = 1; } if( d & 1 ) { if( k & 1 ) if( ok ) puts("1"); else puts("2"); else puts("0"); } else { if( k & 1 ) puts("1"); else if( ok ) puts("0"); else puts("-1"); } } }
signed main() { freopen("wolf.in","r",stdin); freopen("wolf.out","w",stdout);
solve(); }
|