Record a vegetable-driven growth.
Topic links
A look at the topic, I thought you can use the LCA algorithm to write, wrote a half-day, later found not a tree (WOC ...)
Look at the topic, not more than 5 generations, then the direct mob Search,
When querying, mark a person's ancestors for no more than 5 generations, and then search upward from another person for a tagged ancestor within 5 generations.
#include <bits/stdc++.h> using namespace std; #define ALL (v) (v). Begin (), (v). End () #define CL (A, B) memset (A,b,sizeof (a)) #define CLR Clear () #define PB push_back #defin
E MP Make_pair #define FI first #define SE second typedef pair<int,int> PII;
const int MAXN = 100000 + 10;
int SEX[MAXN],VIS[MAXN],USED[MAXN];
vector<int>tree[maxn];
void dfs1 (int x,int cnt) {vis[x] = 1;
if (cnt = = 4) {return;
} for (int i = 0; i < tree[x].size (); i++) {if (!vis[tree[x][i]]) {DFS1 (tree[x][i],cnt+1);
}}} bool Dfs2 (int x,int cnt) {if (cnt = = 5) {return false;
} if (Vis[x]) {return true;
} Used[x] = 1;
BOOL ret = FALSE; for (int i = 0; i < tree[x].size (); i++) {if (!used[tree[x][i]]) {ret = (ret | | DFS2 (tree[x][i],cnt
+1));
}} return ret;
} int main () {int n;
while (~SCANF ("%d", &n)) {int x, y, Z;
Char s[10]; CL (Sex,-1);
for (int i = 0; i < n; i++) {scanf ("%d%s%d%d", &x,s,&y,&z);
SEX[X] = (s[0] = = ' M '? 1:0);
if (Y! =-1) {TREE[X].PB (y);
Sex[y] = 1;
} if (Z! =-1) {TREE[X].PB (z);
Sex[z] = 0;
}} int q;scanf ("%d", &q);
while (q--) {scanf ("%d%d", &x,&y);
if (sex[x] = = Sex[y]) puts ("Never mind");
else {cl (vis,0); cl (used,0);
DFS1 (x,0);
bool ans = DFS2 (y,0); Puts (ans? ")
No ":" Yes ");
}}} return 0;
}