L2-031 Into the Tiger's Den (dfs

topic link

Problem-solving ideas:

Just did something similar this afternoon  

Then I didn’t understand this question and didn’t find the starting point. I thought the starting point was 1.

 

007 Discover that no two roads lead to the same door

In this sentence,    no two roads lead to the same door   , so the number entered after k is definitely not the starting point, so it is necessary to record the number that has not been entered

code show as below:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
vector<int> a[maxn];
vector<int> b;
bool v[maxn];
int ans=0;
int num=0;
void dfs(int d,int x)
{
	int i;
	if(d>ans)
	{
		ans=d;//距离 
		num=x;//距离最远的那个门 
	}
	for(i=0;i<a[x].size();i++)
	{
		dfs(d+1,a[x][i]);//往下遍历 
	}
}
int main()
{
	int n,i,j,k,x,y;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>k;
		while(k--)
		{
			cin>>x;
			v[x]=true;
			a[i].push_back(x);
		}
	}
	for(i=1;i<=n;i++)
	{
		if(!v[i])
		   b.push_back(i);
	}
	for(i=0;i<b.size();i++)
	{
		dfs(1,b[i]);//起点开始 依次遍历 
	}
	cout<<num<<endl;
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43819762/article/details/109787127
dfs