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中Cookie与Set-Cookie头
[原文:http://hi.baidu.com/qinglvzhuye/item/6664a807bb8be3dd73e676d6] android 获取 cookies 有很多办法,但是记住了. h ...
- android studio开发工具的android library打包文件(.aar)本地引用
by 蔡建良 2014-5-13 关键点: 利用Gradle发布本地maven库支持android library 打包文件(*.aar) 的本地引用 开发环境: windows7 64位操作系统 a ...
- Hanoi塔问题
说明:河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edouard Luc ...
- ctype库试运行
from ctypes import * msvcrt=cdll.msvcrt message_string="Hello world!\n" msvcrt.wprintf(&qu ...
- B.xml
pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...
- 代码以兼容高亮方式发布.xml
函数名: abort 功 能: 异常终止一个进程 用 法: void abort(void); 程序例: #include <stdio.h> #include <st ...
- 写在阿里去IOE一周年
[文/ 任英杰] 去年5月17日,阿里巴巴支付宝最后一台IBM小型机在下线,标志着阿里完成去IOE.随后一场去IOE运动不断发酵,甚至传闻IBM中国去年损失了20%的合同额. 去了IOE,奔向何方?阿 ...
- fork()函数详解
linux中fork()函数详解(原创!!实例讲解) (转载) 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程 ...
- SpringMVC + Spring + MyBatis 学习笔记:在类和方法上都使用RequestMapping如何访问
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 先看代码: @Requ ...
- Javascript模板及其中的数据逻辑分离思想(MVC)
#Javascript模板及其中的数据逻辑分离思想 ##需求描述 项目数据库的题目表描述了70-120道题目,并且是会变化的,要根据数据库中的数据描述,比如,选择还是填空题,是不是重点题,题目总分是多 ...