L2-031 深入虎穴 (20分 ,求指点)

//第三和第六个测试点超时,求大佬指点
#include<iostream>
using namespace std;
int f[100000] = {0};
int step=0;
int DFS(int x){
	step++;
	if(f[x]==0) return step;
	else DFS(f[x]);
}
int main()
{
	int N = 0;
	scanf("%d",&N);
	
	int max=0,index=0,a=0;
	for(int i=1;i<=N;i++){
		int k=0;
		scanf("%d",&k);
		if(k==0) continue;
		for(int j=0;j<k;j++){
			a=0;
			scanf("%d",&a);
			f[a]=i;
		}
	} 
		if(N==1){
		printf("1");
		return 0;
	}
	for(int i=1;i<=N;i++){
		step=0;
		int s=DFS(i);
		if(s>max){
			max=s;
			index=i;
		} 
	}
	printf("%d",index);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/g666m666d666/article/details/88976234