Judge Info

  • Memory Limit: 65536KB
  • Case Time Limit: 3000MS
  • Time Limit: 3000MS
  • Judger: Number Only Judger

Description

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 (2<=n<=25) and the number of edges m (1<=m<=25). 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 条边,输入两个点来构成一条边,构成一副无向图,两点之间可以有多条边,从任意一个点搜索,要求每条边只能经过一次,找到最长的路径。

思路:

  1.输入点的个数n, 边的个数m,并且初始化存整个无向图的二维数组w
  2.存边进入二维数组W
  3.对所有点进行dfs搜索,如果路径最长则更新max
  4.dfs找出所有存在的边,并且记录路径的长度

我的代码:

 #include <stdio.h>
#include <string.h> #define MAXN 30 int n,m;
int G[MAXN][MAXN];
int vis[MAXN][MAXN];
int maxNum;
int k; void dfs(int u, int num){
int flag;
for(int v=; v<n; ++v){
if(G[u][v] && !vis[u][v]){ if(G[u][v] != ){
flag = ;
vis[u][v] = ;
vis[u][v] = ;
G[u][v] --;
G[v][u] --;
}
else {
vis[u][v] = ;
vis[v][u] = ; } dfs(v, num+);
if(flag == ){
G[u][v]++;
G[v][u]++;
}
flag = ;
vis[u][v] = vis[v][u] = ;
}
} if(num > maxNum) maxNum = num;
} int main(){ int a,b; while(~scanf("%d %d", &n, &m)){ if(!n && !m) break;
memset(G, , sizeof(G));
for(int i=; i<m; ++i){
scanf("%d %d", &a, &b);
++G[a][b];
++G[b][a];
} maxNum = -; for(int i=; i<n; ++i){
memset(vis, , sizeof(vis));
dfs(i, );
}
printf("%d\n",maxNum);
}
return ;
}

朋友的代码:

 #include <stdio.h>
#include <string.h>
#define N 26 int w[N][N],max,n; //w[N][N] 存入整个图,w[i][j]=x 代表i到j有x条边 void dfs(int,int); int main()
{
int m,sum;
int a,b,i;
while()
{
scanf("%d%d",&n,&m);
if(!m && !n)
break; memset(w,,sizeof(w)); //初始化 for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);
w[a][b]++; //无向图
w[b][a]++;
} max=;
for(i=;i<n;i++)
{
sum=; //把每个点作为出发点,对每一个点进行搜索
dfs(i,sum);
}
printf("%d\n",max);
}
return ;
} void dfs(int a,int sum) // a 为当前探索到哪一个节点,sum为当前探索到的路径长度
{
int i;
if(sum>max) //更新当前搜索到的最长路径
max=sum;
for(i=;i<n;i++)
{
if(w[a][i])
{
w[a][i]--; w[i][a]--; //用去一条边
dfs(i,sum+); // 进行下一层递归
w[a][i]++; w[i][a]++; //回溯到上一层
}
}
return;
}

SZU:D89 The Settlers of Catan的更多相关文章

  1. uva539 The Settlers of Catan

    The Settlers of Catan Within Settlers of Catan, the 1995 German game of the year, players attempt to ...

  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. 7.29 DFS总结

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

  6. UVA题目分类

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

  7. 重拾ZOJ 一周解题

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

  8. 杭电ACM分类

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

  9. HOJ题目分类

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

随机推荐

  1. hdu 5060 War

    War Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. CSS小记(持续更新......)

    1 内联元素和块级元素的区别以及display的三种属性区别 在说起display属性之前,就必须先说说什么是文档流,以及文档流中的内敛元素和块级元素又是指什么? 一直觉得理解了文档流的概念,一堆CS ...

  3. javascript活动

    在javascript倘若有知识的三个方面.事件的第一,流程,其次,事件处理,第三,事件对象.下面就我个人的理解,,分别讲述一下这三个方面的内容. 第一.事件流 事件流指的是事件依照一定的顺序触发.它 ...

  4. POJ1743---Musical Theme(+后缀数组二分法)

    Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are int ...

  5. 大数据系列修炼-Scala课程06

    关于Scala中的正则表达式与模式匹配结合的正则表达式Reg 正则表达式的实现:正则表达式的定义与其它语言差不多,只需在表达式后加一个.r,并且可以遍历相应的表达式进行匹配 //定义的正则表达式 va ...

  6. 读取xml文件"分析 EntityName 时出错"的解决方案

    在涉及到xml与xslt编程的过程中,经常会碰到"分析 EntityName 时出错"的提示,这个不是程序错误,是因为xml文件中使用了一些特殊符号导致的.    XML 节点中不 ...

  7. xml:Invalid byte 2 of 2-byte UTF-8 sequence

    xml解析错误:Invalid byte 2 of 2-byte UTF-8 sequence 在做接口解析时候出现的错误:Invalid byte 2 of 2-byte UTF-8 sequenc ...

  8. VC各种方法获得的窗口句柄

    AfxGetMainWnd AfxGetMainWnd获取窗口句柄本身 HWND hWnd = AfxGetMainWnd()->m_hWnd; GetTopWindow 功能:子窗体z序(Z序 ...

  9. 动画云创始人胥克谦&amp;课程格子创始人李天放分享创业经历

    原文地址:http://student.csdn.net/mcd/topic/163587/955044 2014年10月18日在北京科技大学成功举办了CSDN高校俱乐部全国巡讲,现场參会学生有一百余 ...

  10. java程序连接MongoDB副本集测试

    三个节点有一个节点挂掉也不会影响应用程序客户端对整个副本集的读写! public class TestMongoDBReplSet { public static void main(String[] ...