jzoi#13t1
dfs序真厉害(cena没有o2,vector会炸)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<cstdlib> 7 #include<ctime> 8 #include<vector> 9 #define intree(u,v) (id[u]<=R[v]&&id[u]>=L[v]) 10 #define Rep(i,k) for(int i=first[k];i;i=next[i]) 11 #define lowbit(a) ((a)&(-(a))) 12 #define clr(a,x) memset(a,x,sizeof(a)) 13 #define rep(i,l,r) for(int i=l;i<(r);i++) 14 typedef long long ll; 15 using namespace std; 16 int read() 17 { 18 char c=getchar(); 19 int ans=0,f=1; 20 while(!isdigit(c)){ 21 if(c=='-') f=-1; 22 c=getchar(); 23 } 24 while(isdigit(c)){ 25 ans=ans*10+c-'0'; 26 c=getchar(); 27 } 28 return ans*f; 29 } 30 struct edge{ 31 int u,v; 32 }; 33 const int maxn=2000009; 34 int n,m,dfstime=0,cnt=0,next[maxn],c[maxn],first[maxn],id[maxn],L[maxn],R[maxn],dep[maxn]; 35 edge E[maxn]; 36 int e[maxn<<1]; 37 void dfs(int k,int f){ 38 L[k]=id[k]=++dfstime; 39 Rep(i,k){ 40 if(e[i]!=f){ 41 dep[e[i]]=dep[k]+1; 42 dfs(e[i],k); 43 } 44 } 45 R[k]=dfstime; 46 } 47 int main() 48 { 49 freopen("tree.in","r",stdin); 50 freopen("tree.out","w",stdout); 51 n=read(); 52 rep(i,1,n){ 53 int from=read(),to=read(); 54 e[++cnt]=to; 55 if(!first[from]) first[from]=cnt; 56 next[c[from]]=cnt; 57 c[from]=cnt; 58 e[++cnt]=from; 59 if(!first[to]) first[to]=cnt; 60 next[c[to]]=cnt; 61 c[to]=cnt; 62 E[i].u=from;E[i].v=to; 63 } 64 dfs(1,0); 65 m=read(); 66 while(m--){ 67 int from=read(),to=read(),t=read(); 68 int u=E[t].u,v=E[t].v; 69 if(dep[u]>dep[v]) swap(u,v); 70 if((intree(from,v)&&!intree(to,v))||(!intree(from,v)&&intree(to,v))) 71 puts("NO"); 72 else puts("YES"); 73 } 74 fclose(stdin); 75 fclose(stdout); 76 return 0; 77 }