Description

Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of the N crossroads of the park (labeled from 1 to N). There is no pair of crossroads connected by more than one lane and it is possible to pass from each crossroad to each
other crossroad by a path composed of one or more lanes. A cycle of lanes is simple when passes through each of its crossroads exactly once. 

The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the
administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the
Figure). 



Input

On the first line of the input file the number T of the test cases will be given. Each test case starts with a line with the positive integers N and M, separated by interval (4 <= N <= 4444). Each of the next M lines of the test case contains the labels of
one of the pairs of crossroads connected by a lane.

Output

For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.

Sample Input

1
7 8
3 4
1 4
1 3
7 1
2 7
7 5
5 6
6 2

Sample Output

4

Source




题意就是求最大的环包括点的个数。可是有一个麻烦的地方。就是dfs 到一个点时,怎么高速知道与这个点相连点呢?
后来我知道了 vector 开一个vector数组保存即可了。接下来就是代码了,细节在代码中


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define N 5000 vector<int>q[N]; int vis[N];
int n,m;
int ans; void dfs(int a,int pos)
{
int i;
vis[a]=pos; for(i=0;i<q[a].size();i++)
{
int x=q[a][i]; if(!vis[x])
dfs(x,pos+1);
else
if(vis[a]-vis[x]+1>ans) //比如环 1 2 3 4 1 ,vis[1]=1,vis[4]=4,下一次4与
//1相连。可是已经vis[1],所以环的大小 vis[4]-vis[1]+1
ans=vis[a]-vis[x]+1;
}
} int main()
{
int i,t;
scanf("%d",&t); while(t--)
{
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
int x,y; for(i=1;i<=n;i++) //记得清空上次的数据
q[i].clear(); while(m--)
{
scanf("%d%d",&x,&y);
q[x].push_back(y); //q[x]存与x的临边
q[y].push_back(x); //同理
} ans=0;
for(i=1;i<=n;i++)
if(!vis[i]) //由于一个点就会出现一次。即使两个环有共同边,
//那么在公共边那个分叉点还是会分别进行dfs的
dfs(i,1);
//不加以下会错
if(ans<=2)//一个环须要3个点。可是我郁闷了。假设没有环的话
//怎么会有vis[x]已经标记了呢?(此时ans=0啊)难道有数据相似
// a b b a (⊙o⊙)…
ans=0;
printf("%d\n",ans);
}
return 0;
}

dfs还不是非常熟,可是有事缠身。哎以后一定好好补补dfs



今天听说一种叫做邻接表的东西,竟然用一维数组存储与随意起点相关联的全部边的信息,感觉非常奇妙,不怎么好懂,刚刚入门o(╯□╰)o,也能够用到这个提上


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 4500 int ans,vis[N],head[N];
int n,m,num; struct stud{
int to,next;
}e[N*2]; void build(int u,int v)
{
e[num].to=v;
e[num].next=head[u];
head[u]=num;
num++;
} void dfs(int x,int pos)
{
vis[x]=pos;
int i; for(i=head[x];i!=-1;i=e[i].next)
{
if(vis[e[i].to])
ans=max(ans,vis[x]-vis[e[i].to]+1);
else
dfs(e[i].to,pos+1);
}
}
int main()
{
int i,j,v,u,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head)); num=0;
while(m--)
{
scanf("%d%d",&v,&u);
build(v,u);
build(u,v);
}
memset(vis,0,sizeof(vis));
ans=0; dfs(1,0); if(ans<3) ans=0; printf("%d\n",ans);
}
return 0;
}

POJ 3895 Cycles of Lanes (dfs)的更多相关文章

  1. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  2. poj 3895(求无向图的最大简单环)

    题目链接:http://poj.org/problem?id=3895 思想很简单,就是dfs,并且用一个数组记录到该节点所走过的长度,然后如果遇到已经走过的,就说明存在环了, 更新一下ans. /* ...

  3. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  4. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  5. POJ 2378 Tree Cutting (DFS)

    题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...

  6. poj 1659 Frogs' Neighborhood (DFS)

    http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  7. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  8. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

  9. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

随机推荐

  1. java 基于tcp客户端服务端发送接收数据

    客户端: package demo03; import java.io.IOException; import java.io.InputStream; import java.io.OutputSt ...

  2. hdu 4523(大整数)

    威威猫系列故事——过生日 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  3. Android与H5互调

    前言 微信,微博,微商,QQ空间,大量的软件使用内嵌了H5,这个时候就需要了解Android如何更H5交互的了:有些外包公司,为了节约成本,采用Android内嵌H5模式开发,便于在IOS上直接复用页 ...

  4. codevs——1267 老鼠的旅行(棋盘DP)

    1267 老鼠的旅行 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description ...

  5. javascript解析机制——预解析

    JavaScript解析机制是什么? JavaScript解析过程分为两个阶段,一个是编译阶段,另外一个就是执行阶段. * 编译阶段         编译阶段就是我们常说的JavaScript预解析( ...

  6. ubuntu 配置L2tp出现的问题解决

    运行环境:ubuntu 14.04 64位 配置推荐网址: https://linux.cn/article-3409-1.html 首先,官方教程是必须的: https://help.ubuntu. ...

  7. 鸟哥的linux私房菜服务器架设篇之准备工作和网络基础

    架设服务器的基本功课 1基础网络的基本概念,以方便进行联网和设定及除错 2熟悉操作系统的简易操作:包括登录分析,账号管理,文本编辑器的使用等等的技巧 3信息安全方面:包括防火墙与软件更新方面的相关知识 ...

  8. 尝试使用Osg共享渲染描述表(HGLRC)实现多线程编译显示列表--总结

    在realize()前打开预编译选项指令: osg::DisplaySettings::instance()->setCompileContextsHint(true);    mpr_osgv ...

  9. Confluence JIRA快速入门

    Confluence JIRA快速入门 http://www.confluence.cn/pages/viewpage.action?pageId=2916470

  10. oracle12安装软件后安装数据库,然后需要自己配置监听

    oracle12安装软件后安装数据库,然后需要自己配置监听 没想到你是这样的oracle12: 不能同时安装软件和数据库,分别安装之后,\NETWORD\ADMIN\下面竟然没有listener.or ...