uva539 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的更多相关文章
- SZU:D89 The Settlers of Catan
Judge Info Memory Limit: 65536KB Case Time Limit: 3000MS Time Limit: 3000MS Judger: Number Only Judg ...
- poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
The Settlers of Catan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1123 Accepted: ...
- UVA 539 The Settlers of Catan dfs找最长链
题意:画边求最长链,边不能重复数点可以. 很水,用暴力的dfs即可,因为数据不大. 本来以为可以用floyd进行dp的,后来想想好像不能在有回路上的图跑...于是没去做. #include <c ...
- UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 重拾ZOJ 一周解题
ZOJ 2734 Exchange Cards 题目大意: 给定一个值N,以及一堆卡片,每种卡片有一个值value和数量number.求使用任意张卡片组成N的方式. 例如N = 10 ,cards(1 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 7.29 DFS总结
7.29 黄昏时刻 (一) 全排列 建模: 给了数字n 代表从1-n 个数全排列 思路: 1. 输入n,如果n值为‘0’,则退出程序 2. vis[i] 保存 是否对第i个数字进行访问 3. df ...
随机推荐
- 网络编辑基础:对HTTP协议的头信息详解
HTTP(HyperTextTransferProtocol) 是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP 协议的详细内容请参 考RFC2616.HTTP协议采用了请求/响应模型 ...
- PHP遍历数组
foreach PHP代码: <?php $url = array( '新浪' =>'www.sina.com' , '雅虎' =>'www ...
- Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
mkdir jre cd jre ln -s 你的JDK目录/bin bin 例如:ln -s /usr/lib/jvm/jdk1.8.0_25/bin bin 原文地址:http://www.cnb ...
- [Everyday Mathematics]20150221
设 $y_n=x_n^2$ 如下归纳定义: $$\bex x_1=\sqrt{5},\quad x_{n+1}=x_n^2-2\ (n=1,2,\cdots). \eex$$ 试求 $\dps{\vl ...
- win下 golang 跨平台编译
mac 下编译其他平台的执行文件方式请参看这篇文章,http://www.cnblogs.com/ghj1976/archive/2013/04/19/3030703.html 本篇文章是win下的 ...
- SORT UNIQUE|AGGREGATE|GROUP BY|ORDER BY|JOIN
相信做oracle开发和管理的朋友对sort肯定不会陌生,大家通常都遇到这样那样的排序性能问题,所以我写这一系列关于sort的文章告诉大家在oracle里面sort是怎么一回事以及如果调整sort获得 ...
- TCP经受时延的ACK
下午看<卷1>的时候,感觉“TCP经受时延ACK”这段看的有些迷糊,最后还算理解了,所以这里记下来以免以后又忘了. 经受时延的ACK就是在接收到数据后不立马确认,而是等到内核的一个定时器到 ...
- 用ASP.Net写一个发送ICQ信息的程序
用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...
- 该不该将变量设为 null ?
该不该将变量设为 null ? 对于引用类型的变量,在什么时候需要将其显式设为 null ,在什么时候不需要呢? 局部变量 对于局部变量,在方法结束的时候,变量就会失效,变量指向的对象引用也会减少一个 ...
- SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 使用SpringMVC ...