您的位置:首页 > 其它

UVA - 539 The Settlers of Catan

2016-08-20 23:37 381 查看
题目大意:n 个点 m 条线,每条线只能经过一次,点不做要求,问最多能经过几条线。

解题思路:无向图,处理的时候两个方向都要标记,起点任意,所以循环一下每个点都做一次起点。然后就是回溯了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int n, m;
bool map[50][50];
int ans, tmp;
void dfs(int now) {
int t = 0;
for (int i = 0; i < n; i++) if (map[now][i]) t = 1;
if (!t) {
if (tmp > ans) ans = tmp;
return;
}
for (int i = 0; i < n; i++) {
t = tmp;
if (map[now][i]) {
tmp++;
map[now][i] = false;
map[i][now] = false;
}
else continue;
dfs(i);
map[now][i] = true;
map[i][now] = true;
tmp = t;
}
}
int main() {
while (scanf("%d%d", &n, &m) != EOF && n+m) {
int a, b;
memset(map, false, sizeof(map));
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
map[a][b] = true;
map[b][a] = true;
}

ans = 0;
for (int i = 0; i < n; i++) {
tmp = 0;
dfs(i);
}

printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva