POJ 1330 Nearest Common Ancestors(Tree)

时间:2021-10-25 21:21:55

题目:Nearest Common Ancestors

  根据输入建立树,然后求2个结点的最近共同祖先。

  注意几点:

  (1)记录每个结点的父亲,比较层级时要用;

  (2)记录层级;

  (3)记录每个结点的孩子,vector<int> v[M]写在主函数里面,放在外面超时

代码:

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; const int M = ;
//vector<int> v[M];
int level[M];
int father[M]; void visitEveryone(vector<int> v[], int one, int _level)
{
level[one] = _level;
for(vector<int>::iterator it = v[one].begin(); it != v[one].end(); ++it)
{
visitEveryone(v, *it, _level+);
}
} int main()
{
int T,N,a,b,i,j;
scanf("%d", &T);
while(T--)
{
vector<int> v[M];
scanf("%d", &N);
memset(father, , (N+)*sizeof(int));
for(i = ; i < N; ++i)
{
scanf("%d %d", &a, &b);
father[b] = a;
v[a].push_back(b);
}
scanf("%d %d", &a, &b);
for(j=; father[j] > && j <= N; ++j); visitEveryone(v, j, ); while(a != b)
{ if(level[a] < level[b])
b = father[b];
else
a = father[a];
} printf("%d\n",a);
}
return ;
}