UVA 539-The Settlers of Catan(dfs)

The Settlers of Catan

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 ( )
and the number of edges m ( ). The next mlines 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条边构成的无向无环图,求这个图最长的一条边。

思路:以每个点为顶点,深搜。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
int n,m;
int maxx;
int mp[30][30];
int vis[30][30];
void dfs(int u,int cnt)
{
    int i;
    if(maxx<cnt) maxx=cnt;
    for(i=0; i<n; i++) {
        if(!vis[u][i]&&!vis[i][u]&&mp[u][i]) {
            vis[u][i]=vis[i][u]=1;
            dfs(i,cnt+1);
            vis[u][i]=vis[i][u]=0;//防止对后来的造成影响
        }
    }
}
int main()
{
    int i;
    int u,v;
    while(~scanf("%d %d",&n,&m)) {
        if(!n&&!m) break;
        memset(mp,0,sizeof(mp));
        for(i=0; i<m; ++i) {
            scanf("%d %d",&u,&v);
            mp[u][v]=mp[v][u]=1;
        }
        maxx=0;
        for(i=0; i<n; i++) {
            memset(vis,0,sizeof(vis));
            dfs(i,0);
        }
        printf("%d\n",maxx);
    }
    return 0;
}
时间: 2024-08-18 20:20:14

UVA 539-The Settlers of Catan(dfs)的相关文章

uva 539 The Settlers of Catan(回溯)

uva 539 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 jus

poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))

The Settlers of Catan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1123   Accepted: 732 Description Within Settlers of Catan, the 1995 German game of the year, players attempt to dominate an island by building roads, settlements and c

UVA - 539

  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

uva539 The Settlers of Catan

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 de

The Settlers of Catan

题目链接:The Settlers of Catan 思路: 对每个点都进行一次搜索,保存最长步数即可 代码: #include<iostream> #include<cstdio> #include<cstring> #define MAXN 30 using namespace std; int n,m, G[MAXN][MAXN], vis[MAXN][MAXN], maxNum; void dfs(int u, int num) { for(int v=0; v

uva 219 - Department of Redundancy Department(dfs+剪枝)

题目链接:uva 219 - Department of Redundancy Department 题目大意:给定一些关系,问哪一些关系是可以被替代的,如果可以被替代,给出替代的方案,一种即可. 解题思路:因为总共也就26个字母,所以用二进制数表示状态.剪枝,每次将所有可选关系均考虑进去都无法满足则是false. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

UVa 539 卡坦岛

题意:某游戏的玩家会建造一些公路,公路是连接两个岛的,这样形成一个图.顶点是岛,边是公路,且边长均为1.要求每条边只能走一遍,这样最长的公路是多长. 思路:回溯法.以所有的点都开始枚举一次.由于这里每条边只能访问一次,而顶点是可以访问多次的,所以不能简单地用vis[26]数组来判断,而是用g[][]的值来表示边的条数,这样,输入时边的相应g值++,选择一条边时,则相应g值--.这样没要输出路径,所以连路径都没有保存,直接修改g值就可以了.但不要忘了,dfs之后还需要恢复g值,dfs下面相当于不选

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number