您的位置:首页 > 其它

UVA 539 - The Settlers of Catan

2017-09-06 15:46 567 查看
题目大意:给出n个点,编号从0~n-1。给出m个关系,表示两点之间有连线。求不能走重复线最长的路线为多长。

解题思路:无向图,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: