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. c排序算法大全

    排序算法是一种基本并且常用的算法.由于实际工作中处理的数量巨大,所以排序算法 对算法本身的速度要求很高. 而一般我们所谓的算法的性能主要是指算法的复杂度,一般用O方法来表示.在后面将给出详细的说明.& ...

  2. C#实现不安装Oracle客户端访问远程服务器数据!!

    概述: C#通过使用ADO的方式在未安装Oracle数据库的前提下,客户端程序远程访问服务器,会出现:“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或 ...

  3. jdbc操作数据库返回结果集的注意事项

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  4. T-SQL:SQL Server-数据库查询语句基本查询

    ylbtech-SQL Server-Basic:SQL Server-数据库查询语句基本查询 SQL Server 数据库查询语句基本查询. 1,数据库查询语句基本查询   数据库 SQL Serv ...

  5. Delphi 之前解析串口数据

    //串口接收数据procedure TfrmClientMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Wo ...

  6. 单源最短路径-Dijkstra算法

    1.算法标签 贪心 2.算法描述 具体的算法描述网上有好多,我觉得莫过于直接wiki,只说明一些我之前比较迷惑的. 对于Dijkstra算法,最重要的是维护以下几个数据结构: 顶点集合S : 表示已经 ...

  7. Bluebird-Core API (三)

    Promise.join Promise.join( Promise<any>|any values..., function handler ) – -> Promise For ...

  8. 20151007kaggle Titanic心得

    Titanic是kaggle上一个练手的比赛,kaggle平台提供一部分人的特征,以及是否遇难,目的是预测另一部分人是否遇难.目前抽工作之余,断断续续弄了点,成绩为0.79426.在这个比赛过程中,接 ...

  9. 谈FTP服务器攻击技术及其展望 (修改中)

      欢迎大家给我投票: http://2010blog.51cto.com/350944 请看下篇:谈FTP服务器攻击技术及其展望 (下)              全文完http://chengua ...

  10. rabbitMQ 远程访问

    AMQP server localhost:5672 closed the connection. Check login credentials: Socket closed root@ruiy-c ...