题目链接:

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的更多相关文章

  1. POJ 3895 Cycles of Lanes (dfs)

    Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...

  2. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  3. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  4. Red and Black(简单dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

  6. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  7. POJ 1321 简单dfs

    1.POJ 1321  棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...

  8. 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). ...

  9. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

随机推荐

  1. php 判断是否登录

    <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function _before_index(){ //做 ...

  2. ArcGIS Engine 改变线IPolyline的方向

    有时候需要改变一下线的方向来组成要要的图形,可以按一下方法来变换一下. 如果看官有好的方法的话,请不吝贴上为谢! private IPolyline ChangeDirection(IPolyline ...

  3. STL之Queue(Q)

    STL的Queue(数据结构中的队列): 特点:FIFO 先进先出: 自适应容器(即容器适配器)   栈适配器STL queue  STL中实现的Queue: 用list来实现queue: queue ...

  4. Ext JS学习第四天 我们所熟悉的javascript(三)

    此文用来记录学习笔记: •javascript之函数 •this关键字的使用 –this关键字总是指向调用者,谁调用函数,this就指向谁 •call.apply的使用 –call和apply主要应用 ...

  5. material design是什么?(待以后学习)

    1.它的各种示例:http://blog.csdn.net/cike110120/article/details/46572071 2.它的讲解:http://www.androidchina.net ...

  6. c++,模板函数的定义和使用【初探】

    // demo.cpp : // 模版函数的定义和使用: // 1.模板支持返回参数类型为模板参数. // template <typename RET_T , typename IN1_T , ...

  7. 我的Python成长之路---GitHub使用克隆GitHub(SSH key配置)

    六.克隆GitHub仓库 1.创建仓库目录,目录位置没有要求,比如D:\learngit. 2.配置ssh(如果不配置会每次都输入用户名和密码) 使用TortoiseGit生成ssh-key:开始菜单 ...

  8. Calling 64-bit assembly language functions lodged inside the Delphi source code

    Code: http://www.atelierweb.com/calling-64-bit-assembly-language-functions-lodged-inside-the-delphi- ...

  9. Maven创建项目: Failed to execute goal org.apache.maven.plugin( mvn archetype:create)

    一.概述: 在使用mvn 命令mvn archetype:create -DgroupId=com.chuanliu.c11 -DartifactId=c11searcher在控制创建maven项目和 ...

  10. nginx sendfile tcp_nopush tcp_nodelay参数解释

    sendfile 现在流行的web 服务器里面都提供 sendfile 选项用来提高服务器性能,那到底 sendfile是什么,怎么影响性能的呢?sendfile实际上是 Linux2.0+以后的推出 ...