UVA 539 - The Settlers of Catan
2017-09-06 15:46
567 查看
题目大意:给出n个点,编号从0~n-1。给出m个关系,表示两点之间有连线。求不能走重复线最长的路线为多长。
解题思路:无向图,dfs走,如果两点之间有关系,将关系标记去除,从下一层回来以后再标记,进入下一层,每一层,判断到这个点的最长长度。
ac代码:
解题思路:无向图,dfs走,如果两点之间有关系,将关系标记去除,从下一层回来以后再标记,进入下一层,每一层,判断到这个点的最长长度。
ac代码:
#include <iostream> #include <algorithm> #include <cstring> using namespace std; int n, m, G[30][30], t1, t2; int Max, lenth[30]; void dfs(int u, int len) { for (int v=0; v<n; v++) if (G[u][v]){ G[u][v] = G[v][u] = 0; lenth[u] = max(lenth[u], len); dfs(v, len+1); G[u][v] = G[v][u] = 1; } } int main() { while (scanf("%d%d", &n, &m)!=EOF && n+m){ memset(G, 0, sizeof(G)); memset(lenth, 0, sizeof(lenth)); Max = 0; for (int i=0; i<m; i++){ scanf("%d%d", &t1, &t2); G[t1][t2] = G[t2][t1] = 1; } for (int i=0; i<n; i++) dfs(i, 1); for (int i=0; i<n; i++) Max = max(Max, lenth[i]); printf("%d\n", Max); } return 0; }
相关文章推荐
- UVa 539 - The Settlers of Catan
- The Settlers of Catan(uva539简单的回溯)
- UVA 539 The Settlers of Catan
- uva 539 The Settlers of Catan
- UVA 539 The Settlers of Catan
- UVa - 539 - The Settlers of Catan ( DFS 回溯 )
- 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