题目地址 L2-031 深入虎穴 (25 分)

#include <bits/stdc++.h>
using namespace std;
int n,k,d,Index;

struct Node{
	set<int> list;
}N[100010];

int f[100010];
void BFS(int start){
	queue<int> q;
	q.push(start);
	while(!q.empty()) {
		Index = q.front();
		q.pop();
		for(auto item : N[Index].list) {
			q.push(item);
		}
	}
}

int find(int n) {
	if(f[n] == n) 
		return n;
	else 
		return find(f[f[n]]);
}

void mix(int a,int b) {
	a = find(a);
	b = find(b);
	if(a != b) 
		f[b] = a;
}

int main() {
	scanf("%d", &n);
	for(int i = 1;i <= n; i++) 
		f[i]= i;
	
	for(int i = 1; i <= n; i++) {
		scanf("%d", &k);
		for(int j = 1; j <= k; j++) {
			scanf("%d", &d);
			N[i].list.insert(d);
			mix(i,d);
		}
	}
	BFS(find(f[1]));
	cout<<Index<<endl;
	return 0;
}