简单题
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 10005
bool vis[maxn];
int father[maxn];
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
memset(father, -1, sizeof(father));
memset(vis, 0, sizeof(vis));
int n;
scanf("%d", &n);
int a, b;
for (int i = 0; i < n - 1; i++)
{
scanf("%d%d", &a, &b);
father[b] = a;
}
scanf("%d%d", &a, &b);
while (b != -1)
{
vis[b] = true;
b = father[b];
}
while (!vis[a])
a = father[a];
printf("%d\n", a);
}
return 0;
}