SZU:D89 The Settlers of Catan

 

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找出所有存在的边,并且记录路径的长度

 

我的代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3  
 4 #define MAXN 30
 5  
 6 int n,m;
 7 int G[MAXN][MAXN];
 8 int vis[MAXN][MAXN];
 9 int maxNum;
10 int k;
11  
12 void dfs(int u, int num){
13   int flag;
14     for(int v=0; v<n; ++v){
15         if(G[u][v] && !vis[u][v]){
16  
17             if(G[u][v] != 1){
18                 flag = 1;
19                 vis[u][v] = 0;
20                 vis[u][v] = 0;
21                 G[u][v] --; 
22                 G[v][u] --;
23             }
24             else {
25                 vis[u][v] = 1;
26                 vis[v][u] = 1;
27          
28             }
29          
30             dfs(v, num+1);
31             if(flag == 1){
32                 G[u][v]++;
33                 G[v][u]++;
34             }
35             flag = 0;
36             vis[u][v] = vis[v][u] = 0;
37         }
38     }
39  
40     if(num > maxNum) maxNum = num;
41 }
42  
43  
44 int main(){
45  
46     int a,b;
47  
48     while(~scanf("%d %d", &n, &m)){
49  
50         if(!n && !m) break;
51         memset(G, 0, sizeof(G));
52         for(int i=0; i<m; ++i){
53             scanf("%d %d", &a, &b);
54             ++G[a][b];  
55         ++G[b][a];
56         }
57  
58         maxNum = -9999;
59  
60         for(int i=0; i<n; ++i){
61             memset(vis, 0, sizeof(vis));
62             dfs(i, 0);
63         }
64         printf("%d\n",maxNum);
65     } 
66     return 0;
67 }
View Code

 

 

朋友的代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 26
 4  
 5 int w[N][N],max,n;  //w[N][N] 存入整个图,w[i][j]=x 代表i到j有x条边
 6  
 7 void dfs(int,int);
 8  
 9 int main()
10 {
11     int m,sum;
12     int a,b,i;
13     while(1)
14     {        
15         scanf("%d%d",&n,&m);
16         if(!m && !n)
17             break;
18  
19         memset(w,0,sizeof(w)); //初始化
20  
21         for(i=0;i<m;i++)
22         {
23             scanf("%d%d",&a,&b);
24             w[a][b]++;  //无向图
25             w[b][a]++;
26         }
27  
28         max=0; 
29         for(i=0;i<n;i++)
30         {
31             sum=0;   //把每个点作为出发点,对每一个点进行搜索
32             dfs(i,sum);
33         }
34         printf("%d\n",max);
35     }
36     return 0;
37 }
38  
39 void dfs(int a,int sum)  // a 为当前探索到哪一个节点,sum为当前探索到的路径长度
40 {
41     int i;
42     if(sum>max)  //更新当前搜索到的最长路径
43         max=sum;
44     for(i=0;i<n;i++)
45     {
46         if(w[a][i])
47         {
48             w[a][i]--;  w[i][a]--; //用去一条边
49             dfs(i,sum+1);  // 进行下一层递归
50             w[a][i]++;  w[i][a]++; //回溯到上一层
51         }
52     }
53     return;
54 }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值