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 539 - The Settlers of Catan
- Uva 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- UVA 539 The Settlers of Catan
- UVA 539 - The Settlers of Catan 解题报告
- UVA539- The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan, 简单回溯
- UVA 539 The Settlers of Catan(图的搜索)
- UVA 539 The Settlers of Catan
- UVA 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVA 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVA 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- uva 539 The Settlers of Catan
- UVA 539 - The Settlers of Catan(深搜 回溯)
- UVA 539 The Settlers of Catan(回溯法)
- uva 539 The Settlers of Catan