POJ 3895 Cycles of Lanes (dfs)
Description
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
one of the pairs of crossroads connected by a lane.
Output
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
#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
#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)的更多相关文章
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- poj 3895(求无向图的最大简单环)
题目链接:http://poj.org/problem?id=3895 思想很简单,就是dfs,并且用一个数组记录到该节点所走过的长度,然后如果遇到已经走过的,就说明存在环了, 更新一下ans. /* ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- POJ 2378 Tree Cutting (DFS)
题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- poj 2531 Network Saboteur( dfs )
题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
随机推荐
- 关于URL编码 [转]
转自: http://www.ruanyifeng.com/blog/2010/02/url_encoding.html 作者: 阮一峰 日期: 2010年2月11日 一.问题的由来 URL就是网址, ...
- JdbcTemplate模板使用
1.添加模板的配置文件 在spring的配置文件中加入如下代码 <bean class="org.springframework.jdbc.core.JdbcTemplate" ...
- AC日记——[中山市选2009]谁能赢呢? bzoj 2463
2463 思路: 博弈: 把先手和后手的走的两个格子看做一个1*2的方格: 如果n为偶数,那么棋盘一定可以被1*2的方格覆盖: 前端为先手,后端为后手: 那么,当还剩下一个1*2的方格时,先手一定可以 ...
- (6)ASP.NET HttpServerUtility 类
HttpServerUtility 类 提供用于处理 Web 请求的 Helper 方法 https://msdn.microsoft.com/zh-cn/library/system.web.htt ...
- (17)python Beautiful Soup 4.6
一.安装 1.登陆官网:https://www.crummy.com/software/BeautifulSoup/ 2.下载 3.解压 4.安装 cmd找到文件路径,运行 setup.py buil ...
- n!在k进制下的后缀0
问n! 转化成k进制后的位数和尾数的0的个数.[UVA 10061 How many zeros and how many digits?] Given a decimal integer numbe ...
- Algorithm | Vector
因为平常用的话只是vector的一些非常简单的功能,基本上把它当数组来用,现在也只是把这一部分写了一些. template<class T> class XVector { public: ...
- MariaDB半同步复制
1.主从复制原理 MySQL的二进制日志(binglog)会记录所有对数据库进行更改的操作,也就是说只要是会对数据库产生修改的操作都会被记录到二进制日志中去.记录二进制日志的主要目的有两方面:a.恢复 ...
- luogu P1880 石子合并
题目描述 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...
- Linux下DNW源码及安装
平台: Ubuntu12.04 + 64bit tiny4412ADK 1.首先要安装libusb-dev这个库,执行“sudo apt-get install libusb-dev”,装完之后就编译 ...