The Settlers of Catan

Within Settlers of Catan, the 1995 German game of the year, players attempt to dominate an island by building roads, settlements and cities across its uncharted wilderness.

You are employed by a software company that just has decided to develop a computer version of this game, and you are chosen to implement one of the game's special rules:

When the game ends, the player who built the longest road gains two extra victory points.

The problem here is that the players usually build complex road networks and not just one linear path. Therefore, determining the longest road is not trivial (although human players usually see it immediately).

Compared to the original game, we will solve a simplified problem here: You are given a set of nodes (cities) and a set of edges (road segments) of length 1 connecting the nodes. The longest road is defined as the longest path within the network that doesn't use an edge twice. Nodes may be visited more than once, though.

Example: The following network contains a road of length 12.

o        o -- o        o
\ / \ /
o -- o o -- o
/ \ / \
o o -- o o -- o
\ /
o -- o

Input

The input file will contain one or more test cases.

The first line of each test case contains two integers: the number of nodes n ( ) and the number of edges m ( ). The next m lines describe the m edges. Each edge is given by the numbers of the two nodes connected by it. Nodes are numbered from 0 to n-1. Edges are undirected. Nodes have degrees of three or less. The network is not neccessarily connected.

Input will be terminated by two values of 0 for n and m.

Output

For each test case, print the length of the longest road on a single line.

Sample Input

3 2
0 1
1 2
15 16
0 2
1 2
2 3
3 4
3 5
4 6
5 7
6 8
7 8
7 9
8 10
9 11
10 12
11 12
10 13
12 14
0 0

Sample Output

2
12

 

// 题意:输入n个结点和m条边的无向图(不一定连通),求最长路的长度。边不能经过两次,但是顶点可以重复经过

// 限制:2<=n<=25, 1<=m<=25,没有自环和重边

// 算法:DFS

 

dfs中更新深度d

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=26;
int a[maxn][maxn];
int n, m;
int ans; void dfs(int i, int d)
{
for(int j=0; j<n; j++) if(a[i][j]) {
a[i][j]=0;
a[j][i]=0;
dfs(j, d+1);
a[i][j]=1;
a[j][i]=1;
}
ans=max(ans, d);
} int solve()
{
ans=0;
for(int i=0;i<n;i++)
dfs(i, 0);
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva539.in", "r", stdin);
#endif
while(scanf("%d%d", &n, &m)==2 && (n || m)) {
memset(a, 0, sizeof(a));
for(int i=0;i<m;i++) {
int x,y;
scanf("%d%d", &x, &y);
a[x][y]=1;
a[y][x]=1;
}
printf("%d\n", solve());
} return 0;
}

dfs求长度另一种写法: dfs返回最长长度

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=26;
int G[maxn][maxn];
int n, m; int dfs(int i)
{
int len=0;
for(int j=0; j<n; j++) if(G[i][j]) {
G[i][j]=G[j][i]=0;
len=max(len, dfs(j)+1);
G[i][j]=G[j][i]=1;
}
return len;
} int solve()
{
int ans=0;
for(int i=0;i<n;i++)
ans=max(ans, dfs(i));
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva539.in", "r", stdin);
#endif
while(scanf("%d%d", &n, &m)==2 && (n || m)) {
memset(G, 0, sizeof(G));
for(int i=0;i<m;i++) {
int x,y;
scanf("%d%d", &x, &y); G[x][y]=G[y][x]=1;
}
printf("%d\n", solve());
}
return 0;
}

uva539 The Settlers of Catan的更多相关文章

  1. SZU:D89 The Settlers of Catan

    Judge Info Memory Limit: 65536KB Case Time Limit: 3000MS Time Limit: 3000MS Judger: Number Only Judg ...

  2. poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))

    The Settlers of Catan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1123   Accepted: ...

  3. UVA 539 The Settlers of Catan dfs找最长链

    题意:画边求最长链,边不能重复数点可以. 很水,用暴力的dfs即可,因为数据不大. 本来以为可以用floyd进行dp的,后来想想好像不能在有回路上的图跑...于是没去做. #include <c ...

  4. UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索

    UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. 重拾ZOJ 一周解题

    ZOJ 2734 Exchange Cards 题目大意: 给定一个值N,以及一堆卡片,每种卡片有一个值value和数量number.求使用任意张卡片组成N的方式. 例如N = 10 ,cards(1 ...

  7. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  8. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  9. 7.29 DFS总结

    7.29   黄昏时刻 (一) 全排列 建模: 给了数字n 代表从1-n 个数全排列 思路: 1. 输入n,如果n值为‘0’,则退出程序 2. vis[i] 保存 是否对第i个数字进行访问 3. df ...

随机推荐

  1. DB2因表空间不够产生load表失败

    今天下午恢复表的时候发现出现错误: SQL3520W  Load Consistency Point was successful. SQL3110N  The utility has complet ...

  2. 使用 XML 实现 REST 式的 SOA

    什么是 SOA? 如果公司有大量应用程序,这些程序供不同部门的承担不同责任的职员使用,那么就适合使用面向服务体系结构(Service Oriented Architecture,SOA).这些应用程序 ...

  3. 平庸与卓越的差别 z

    本文是清华大学陈吉宁校长于在 2015 年第一次研究生毕业典礼暨学位授予仪式上的讲话,原文标题:选择与坚持.演讲非常精彩,值得您细细阅读. 亲爱的同学们: 今天,共有 1318 名同学获得博士.硕士学 ...

  4. go 应用程序性能测试

    runtime/pprof 我们要加入对pprof包里的方法调用,程序才能将运行时候程序的堆内存分配状态记录到文件(也可以是写到其他地方,例如网络等)中,以便进一步的分析. 如果你的go程序只是一个应 ...

  5. 搭建Eclipse、Resin Web开发环境

    搭建Eclipse.Resin Web开发环境 一.当然是安装java开发环境 参看: Java环境的搭建 http://www.cnblogs.com/ghj1976/archive/2010/04 ...

  6. HTTP协议的状态码

    对于Web编程人员来说,熟悉了解HTTP协议的状态码是很有必要的,很多时侯可能根据HTTP协议的状态码很快就能定位到错误信息!今天整理了一下所有HTTP状态码. HTTP状态码(HTTP Status ...

  7. Groovy获取json和xml数据

    如果是xml就用这个 // to read a node from your Response def grUtils = new com.eviware.soapui.support.GroovyU ...

  8. linux 学习网站

    study-Area:http://www.study-area.org 鸟哥的私房菜:http://linux.vbird.org 鸟哥的私房菜课后答案:http://wapwenku.baidu. ...

  9. Android开源图表库介绍

    XCL-Charts XCL-Charts V1.8     Android开源图表库(XCL-Charts is a free charting library for Android platfo ...

  10. mybatis系列-16-spring和mybatis整合

    16.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSess ...