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. Redis 主从 Replication 的配置,主从复制原理

    概述 Redis的replication机制允许slave从master那里通过网络传输拷贝到完整的数据备份.具有以下特点: 异步复制.从2.8版本开始,slave能不时地从master那里获取到数据 ...

  2. nagios高可用性设置

    1. 前言 如何来实现nagios监控系统的高可用,监控是很重要的,在关键时刻进行通知报警,通知人员进行相应的处理. 在进行配置的时候,需要配置两台相同服务的nagios服务器,配置相同,同时在运行, ...

  3. 【LeetCode】70 - Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. js动画框架设计

    当你不再依赖JQuery时,当你已经厌倦了引入js类库实现一些动画效果的方式,当你想实现一个简单而实用的动画框架......下面介绍下愚人设计的动画框架:支持动画缓动算法函数,如Linear.Cubi ...

  5. linux 条件变量

    互斥量就是一把锁,在访问数据时能保证同一时间内只有一个线程访问数据,在访问完以后再释放互斥量上的锁. 条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条 ...

  6. PHP代码格式化批量脚本

    @echo off echo please input phpCB url: set /p input= cd /d "E:\tools\phpCB\" phpCB --space ...

  7. poj 1552 Doubles

    #include <stdio.h> #include <stdlib.h> ]; int cmp(const void *a, const void *b) { return ...

  8. URAL-1997 Those are not the droids you're looking for 二分匹配

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997 题意:记录了n个人进出门的时间点,每个人在房子里面待的时间要么小于等于a,要么大于 ...

  9. opengpg

  10. 如何用 redis 造一把分布式锁

    基本概念 锁 wiki:In computer science, a lock or mutex (from mutual exclusion) is a synchronization mechan ...