提交时间:2022-07-13 11:52:03
运行 ID: 51547
#include<bits/stdc++.h> using namespace std; const int MAXN=100005; inline int read() { int x=0,c=getchar(),f=1; for(; c<=47||c>=58; c=getchar())f=f&&(c^45); for(; c>=48&&c<=57; c=getchar())x=(x<<3)+(x<<1)+(c&15); return f?x:-x; } bitset<MAXN>s[MAXN]; struct Edge { int v,w,next; } edge[MAXN<<1]; int n,m,head[MAXN],ecnt; void addedge(int u,int v,int w) { edge[++ecnt].v=v; edge[ecnt].w=w; edge[ecnt].next=head[u]; head[u]=ecnt; } void dfs(int u,int fa) { for(int i=head[u],v; i; i=edge[i].next) { v=edge[i].v; if(v==fa)continue; s[v]=s[u]; s[v][edge[i].w]=s[v][edge[i].w]^1; dfs(v,u); } } int solve(int u,int v,int r) { int l=r; while(l&&!(s[u][l]^s[v][l]))--l; return r-l; } int main() { scanf("%d",&n); for(int i=1,u,v,w; i<n; ++i) { u=read(),v=read(),w=read(); addedge(u,v,w),addedge(v,u,w); } dfs(1,-1); m=read(); for(int i=1,u,v,r; i<=m; ++i) { u=read(),v=read(),r=read(); printf("%d\n",solve(u,v,r)); } return 0; }