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;
}

最新文章

  1. CentOS:设置系统级代理(转)
  2. java 访问sql server数据库
  3. html元素背景样式大小调整
  4. webView用法小结
  5. mysql 不能远程连接
  6. Dubbo源码学习--集群负载均衡算法的实现
  7. PLSQL学习教程(全)
  8. mysql用户链接数
  9. Swift与C++混编 OpenCV初体验 图片打码~
  10. IIS部署ASP.Net Core 502.5错误和解决
  11. 第一章 Hyper-V 2012 R2角色部署
  12. Rpgmakermv(31)MOG插件与YEP的结合
  13. HttpServletRequest.getInputStream() 只能读取一次
  14. 1 python基础知识
  15. Ubuntu清理系统垃圾 命令
  16. 【BIRT】02_开发一张简单的报表
  17. PCB封装技术
  18. 前端 webpack
  19. java学习(四)static静态变量 和this
  20. Qt WebRTC demo

热门文章

  1. UVA11080- Place the Guards(二分图染色)
  2. Android采用Volley具体的例子展示完整的异步加载数据(一)
  3. Instruments-Automation: 通过命令行执行测试用例
  4. jsp页面onsubmit=&amp;quot;return checklogin();&amp;quot;该解决方案给
  5. 准备战争“软测试”之DB基础知识
  6. Android 自己的自动化测试(5)&amp;lt;robotium&amp;gt;
  7. 【百度地图API】如何利用自己的数据制作社交地图?只显示可视区域内的标注
  8. 【百度地图API】如何根据摩卡托坐标进行POI查询,和计算两点距离
  9. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单
  10. 新手sqlserver数据库dba需要注意的小细节