UVA 539 dfs最长路径

#include<bits/stdc++.h>

using namespace std;

//#define LOCAL
#define MAXN 30

int road[MAXN][MAXN];
int m , n;
int maxLength;
 void dfs(int s  ,int num);
 
int main()
{
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	while(cin>>n>>m)
	{
		if(n==0&&m==0) break;
		memset(road,0,sizeof(road));
		int numNumber = m;
		maxLength = 0;
		while(m--)
		{
			int from ,to;
			cin>>from>>to;
			road[from][to] = 1;
			road[to][from] = 1;
		}
		for(int i = 0 ;i <n;i++)
		{	
			dfs(i,0);
		}
		
		cout << maxLength << endl;
	}
	
	
	return 0;
 } 
 
 void dfs(int s  ,int num)
 {
 	for(int i = 0 ;i <n;i++)
 	{
  		if(road[s][i] == 1)
  		{
  			road[s][i] = 0;
			road[i][s] = 0;
  			dfs(i,num+1);	
  			road[s][i] = 1;
			road[i][s] = 1;
		}	
	}
//	cout << num ;
	if(num >maxLength)
	maxLength = num ; 
 }

猜你喜欢

转载自blog.csdn.net/xxhbenren/article/details/84327484
今日推荐