UVA - 539 The Settlers of Catan

来源:互联网 发布:程序员的数学百度网盘 编辑:程序博客网 时间:2024/06/11 17:05

题目大意: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; }
0 0
原创粉丝点击