poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 1123 | Accepted: 732 |
Description
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 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
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
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> using namespace std; int map[30][30];
bool vis[30][30];
int n, m;
int MAX; void dfs(int dd, int num )
{
for(int i=0; i<n; i++)
{
if(map[dd][i]==1 && vis[dd][i]==0 )
{
vis[dd][i]=1; vis[i][dd]=1;
dfs(i, num+1);
vis[dd][i]=0; vis[i][dd]=0;
}
}
if(num>MAX) MAX=num;
} int main()
{
int i, j;
int u, v;
while(scanf("%d %d", &n, &m)!=EOF)
{
if(n==0 && m==0 )break;
memset(map, 0, sizeof(map )); for(i=0; i<m; i++)
{
scanf("%d %d", &u, &v);
map[u][v]=1; map[v][u]=1;
}
MAX=-1;
int cnt;
for(i=0; i<n; i++)
{
cnt=0;
memset(vis, false, sizeof(vis));
dfs(i, 0);
//if(cnt>MAX) MAX=cnt;
}
printf("%d\n", MAX );
}
return 0;
}
poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))的更多相关文章
- Floyd-Warshall求图中任意两点的最短路径
原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...
- HDU 3249 Test for job (有向无环图上的最长路,DP)
解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...
- hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜
题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...
- POJ 3660 Cow Contest(求图的传递性)
题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...
- hdu4587 Two Nodes 求图中删除两个结点剩余的连通分量的数量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题目给了12000ms,对于tarjan这种O(|V|+|E|)复杂度的算法来说,暴力是能狗住的 ...
- 编程之美 set 7 求数组中的最长递增子序列
解法 1. 假设在目标数组 array[] 的前 i 个元素中, 最长递增子序列的长度为 LIS[i] 那么状态转移方程为 LIS[i] = max(1, LIS[k]+1) array[i+1] & ...
- poj 1679 The Unique MST 【次小生成树+100的小数据量】
题目地址:http://poj.org/problem?id=1679 2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2 Sample Outpu ...
- splunk中mongodb作用——存用户相关数据如会话、搜索结果等
About the app key value store The app key value store (or KV store) provides a way to save and retri ...
- SpringBoot中Post请求提交富文本数据量过大参数无法获取的问题
yml增加配置 # 开发环境配置 server: tomcat: max-http-form-post-size: -1
随机推荐
- Redis的安装和环境的搭建并设置服务(Redis学习笔记一)
由于Redis在win上安装实在是太过于麻烦.我们选择把redis安装部署在linux上,然后远程连接. 安装Redis (1)cd /usr/src 进入下载目录 (1) yum install - ...
- MySQL ERROR 1044 (42000) 解决方法
在Terminal中输入 mysql 进入到数据库命令行,然后直接: CREATE DATABASE IF NOT EXISTS yuntu; 结果出现如下错误: ERROR 1044 (42000) ...
- DFRobot万物互联大赛第二轮
前言 最近放在阳台的花草被啥东西给吃了,然后厨房挂在墙上的小虾米也不知道咋的被抓破吃光了(我怀疑是隔隔壁两条泰迪),所以打算做个简单的项目,教训一下偷吃贼.时间比较仓促,内容比较多,能力有比较有限,好 ...
- 应用设置Setting的实现
有非常多应用都在iOS设置中有相关的设置.例如以下图: 通过这个设置能够方便的相应用的一些主要的设置进行更改. 要完整的实现这个设置功能,有下面几方面问题须要解决: 1)设置的编写(实现设置的 ...
- iframe 实现网页本页显示
<el-dialog title="" :visible.sync="dialogVisible"> <iframe src="ht ...
- 解决apt 依赖破损的问题
在 安装 python3 / python2 共存的环境时, 不知道做了什么, 导致 apt 依赖故障 odoo@sy-odoo-08:~$ sudo apt-get remove apport 正在 ...
- solr入门之pinyin4j源代码改写动态加入扩展词及整合进war项目中
1.初始化时载入用户定义的字典 package net.sourceforge.pinyin4j; import net.sourceforge.pinyin4j.multipinyin.Trie; ...
- cocos2d-x-lua基础系列教程五(lua单例)
lua-单例 function newAccount(initlizedBanlance) local self = {balance = initlizedBanlance} local show ...
- 设计一个线程安全的单例(Singleton)模式
在设计单例模式的时候.尽管非常easy设计出符合单例模式原则的类类型,可是考虑到垃圾回收机制以及线程安全性.须要我们思考的很多其它.有些设计尽管能够勉强满足项目要求,可是在进行多线程设计的时候.不考虑 ...
- 在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...