同时也要记录父母的性别,输出询问时要用到
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <stdbool.h> 5 #include <math.h> 6 #define maxn 100000+1 7 8 long fa[maxn][2]; 9 long f[2][100]; 10 11 char sex[maxn]; 12 long a,b; 13 bool v; 14 15 bool pan() 16 { 17 long i,j; 18 for (i=1;i<=f[0][0];i++) 19 for (j=1;j<=f[1][0];j++) 20 if (f[0][i]==f[1][j]) 21 return false; 22 return true; 23 } 24 25 void dfs(long d,long remain,long mode) 26 { 27 if (remain==0) 28 return; 29 30 f[mode][0]++; 31 f[mode][f[mode][0]]=d; 32 33 if (fa[d][0]!=-1) 34 dfs(fa[d][0],remain-1,mode); 35 if (fa[d][1]!=-1) 36 dfs(fa[d][1],remain-1,mode); 37 } 38 39 int main() 40 { 41 struct node *p; 42 long n,m,i,num; 43 char c; 44 for (i=0;i<100000;i++) 45 { 46 fa[i][0]=-1; 47 fa[i][1]=-1; 48 } 49 scanf("%ld",&n); 50 for (i=1;i<=n;i++) 51 { 52 scanf("%ld",&num); 53 scanf("%c",&c); 54 scanf("%c",&sex[num]); 55 scanf("%c",&c); 56 scanf("%ld%ld",&fa[num][0],&fa[num][1]); 57 sex[fa[num][0]]='M'; 58 sex[fa[num][1]]='F'; 59 } 60 scanf("%ld",&m); 61 while (m) 62 { 63 m--; 64 scanf("%ld%ld",&a,&b); 65 if (sex[a]==sex[b]) 66 { 67 printf("Never Mind "); 68 continue; 69 } 70 71 f[0][0]=0; 72 dfs(a,5,0); 73 74 f[1][0]=0; 75 dfs(b,5,1); 76 77 if (pan()) 78 printf("Yes "); 79 else 80 printf("No "); 81 } 82 return 0; 83 }