UVA-539
题意:每条边只能走一次,求最多能走过多少条边。
解题思路:枚举一个顶点进入dfs,dfs中找还能走的边进行尝试,不知道两个城市之间是否存在多条边,但是记录总数和边走过的次数来控制就不用担心了。记录走的最大边数。最后输出。
/*************************************************************************
> File Name: UVA-539.cpp
> Author: Narsh
>
> Created Time: 2016年07月27日 星期三 11时12分39秒
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int map[30][30],n,m,Max;
int pd[30][30];
void dfs(int x, int dis) {
if (dis > Max) Max=dis;
for (int i = 0; i < n; i++)
if (pd[x][i] < map[x][i]) {
pd[x][i]++;
pd[i][x]++;
dfs(i,dis+1);
pd[x][i]--;
pd[i][x]--;
}
}
int main() {
while (scanf("%d%d",&n,&m) && n+m) {
memset(pd,0,sizeof(pd));
memset(map,0,sizeof(map));
for (int i = 1; i <= m; i++) {
int a,b;
scanf("%d%d",&a,&b);
map[a][b]++;
map[b][a]++;
}
Max=0;
for (int i = 0; i < n; i++)
dfs(i,0);
printf("%d\n",Max);
}
}