POJ_2258 The settlers of Catan (DFS)

  /*这题应该属于水题吧,基本不用什么大的处理,看懂题意就行。直接n次dfs,找出最大的length*/

//My Code: 16MS

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 30;

int map[N][N];
bool vis[N][N];
int n;
int ans;

void dfs(int now, int t){
int f, i;
for(f = 1, i = 0; i < n; i++){
if(vis[now][i] == false && map[now][i]){
f = 0;
}
}
//cout << f << endl;
if(f) {ans = ans > t ? ans : t; return;}
for(i = 0; i < n; i++){
if(map[now][i] && !vis[now][i]){
vis[now][i] = vis[i][now] = true;
dfs(i, t+1);
vis[now][i] = vis[i][now] = false;
}
}
}

int main(){
//freopen("data.in", "r", stdin);

int m, max, x, y, i;
while(~scanf("%d%d", &n, &m)){
if(n == 0 && m == 0) break;
memset(map, 0, sizeof(map));
while(m--){
scanf("%d%d", &x, &y);
map[x][y] = map[y][x] = 1;
}
for(max = -1, i = 0; i < n; i++){
memset(vis, false, sizeof(vis));
ans = 0; dfs(i, 0);
if(max < ans)
max = ans;
}
printf("%d\n", max);
}
}



posted @ 2011-10-21 14:57  AC_Von  阅读(232)  评论(0编辑  收藏  举报