poj-3895-Cycles of Lanes 简单DFS
题目链接:
http://poj.org/problem?id=3895
题目意思:
在无向连通图中图中找一个经过边数最多的环。
解题思路:
从任意一点直接DFS,不用回溯,注意构成环的话至少有3条边。
因为任意一个最大环,一定可以搜到。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
using namespace std; int ans;
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/
bool flag[5500];
struct Edge
{
int v;
struct Edge * next;
}*head[555500],edge[555500];
int n,m,cnt;
int num[4500]; void add(int a,int b)
{
cnt++;
edge[cnt].v=b,edge[cnt].next=head[a];
head[a]=&edge[cnt];
} void dfs(int cur,int hav)
{
struct Edge *p=head[cur];
flag[cur]=true;
num[cur]=hav;
while(p)
{
if(flag[p->v]) //下一步已经访问过了,说明有环
{
if(hav+1-num[p->v]>ans) //往回走的话就是2
ans=hav+1-num[p->v];
}
else
dfs(p->v,hav+1);
p=p->next;
}
// num[cur]=0; //不需要回溯
//flag[cur]=false;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,NULL,sizeof(head));
memset(flag,false,sizeof(flag));
memset(num,0,sizeof(num));
cnt=0;
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b),add(b,a); }
num[1]=0;
ans=0;
dfs(1,0);
if(ans < 3) //凑成环的话,至少要三条边
ans = 0;
printf("%d\n",ans);
}
return 0;
}
poj-3895-Cycles of Lanes 简单DFS的更多相关文章
- POJ 3895 Cycles of Lanes (dfs)
Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...
- POJ 1979 Red and Black (简单dfs)
题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- Red and Black(简单dfs)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- POJ1573(Robot Motion)--简单模拟+简单dfs
题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...
- POJ 1321 简单dfs
1.POJ 1321 棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...
- poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)
http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...
- POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)
题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...
随机推荐
- linux下的压缩解压命令 tar 的简单描述
命令名称:tar 命令所在路径:/bin/tar 语法:tar选项·「-zcf」·「压缩后文件名」「目录」 -c 打包 -v 显示详细信息 -f 指定文件名 -z 打包同时压缩 tar命令解压缩语法: ...
- poj 3053 Fence Repair(优先队列)
题目链接:http://poj.org/problem?id=3253 思路分析:题目与哈夫曼编码原理相同,使用优先队列与贪心思想:读入数据在优先队列中,弹出两个数计算它们的和,再压入队列中: 代码如 ...
- 一组开源 HTML5 Apps
一组用"画app吧"开发的 HTML5 Apps,默认使用FirefoxOS设备,其实它们都可以在像Android/IPhone/WindowsPhone8/BlackBerry/ ...
- Hadoop Security Authentication Terminology --Kerberos
Hadoop Security Authentication Terminology --Kerberos What is kinit? Kinit - obtain and cache Kerbe ...
- source code of MES Data
<HTML> <HEAD> <TITLE>TELOGS</TITLE> </HEAD> <BODY> <?php /* c ...
- BZOJ 1977: [BeiJing2010组队]次小生成树 Tree( MST + 树链剖分 + RMQ )
做一次MST, 枚举不在最小生成树上的每一条边(u,v), 然后加上这条边, 删掉(u,v)上的最大边(或严格次大边), 更新答案. 树链剖分然后ST维护最大值和严格次大值..倍增也是可以的... - ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- XML IList<T> TO DataSet TO DataTable 相互转换
//遍历XML 获得 DataSet //XmlTextReader static void Main(string[] args) { string xmlData = @"D:\stud ...
- python读写xml
来自http://blog.csdn.net/liuyuehui110/article/details/7287897 备份防止链接失效 一.XML的读取. 在 NewEdit 中有代码片段的功能,代 ...
- SpringMVC入门二: 1规范结构, 2简单整合MyBatis
昨天拿springMVC写的helloworld结构不好, 这次先调整一下体系结构 , 然后简单整合一下MyBatis spring的配置还是以注解为主, 不过MyBatis的映射文件什么的还是拿xm ...