题目链接:
L2-031 深入虎穴 (25分)
思路:
bfs一下即可
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, par[maxn];
vector<int> son[maxn];
void bfs(int u){
int now;
queue<int> que;
que.push(u);
while(!que.empty()){
now = que.front();
que.pop();
for(int & x : son[now]) que.push(x);
}
printf("%d", now);
}
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
scanf("%d", &n);
for(int i = 1; i <= n; i++){
int k, d;
scanf("%d", &k);
while(k--){
scanf("%d", &d);
son[i].push_back(d);
par[d] = i;
}
}
int root = 1;
while(par[root]) root = par[root];
bfs(root);
return 0;
}