POJ 3895 Cycles of Lanes(dfs+模拟)
来源:互联网 发布:sha1 js 时间 编辑:程序博客网 时间:2024/06/05 23:45
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).
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).
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.
For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.
1 7 8 3 4 1 4 1 3 7 1 2 7 7 5 5 6 6 2
4
题解:
这题比赛中做出来的第一题,想法就是一次性的dfs,这个要意会。。在代码中看看吧
代码:
#include<algorithm>#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<deque>using namespace std;struct edge{ int f,t;};int a[4500][105];//保存每个节点对应的边的编号edge b[4500];//保存每个边int num[4500];//保存每个点连接边的数目int vis[4500];//保存每个边是否被访问过int dis[4500];//保存每个节点的最后位置int maxx,n,m;void dfs(int index,int step){ if(dis[index]) { int t=step-dis[index]; if(maxx<t) { maxx=t; } step=dis[index];//回到了原处要回溯一下位置信息 } else dis[index]=step;//没有经过就记录一下最初位置 for(int i=0;i<num[index];i++) { if(!vis[a[index][i]])//边没遍历过就走一遍 { vis[a[index][i]]=1; int d=((index==b[a[index][i]].f)?b[a[index][i]].t:b[a[index][i]].f);//d得出来的是该节点通往何处 dfs(d,step+1); } }}int main(){ int i,j,test,x,y; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); memset(num,0,sizeof(num)); memset(dis,0,sizeof(dis)); for(i=0;i<m;i++) { scanf("%d%d",&x,&y); b[i].f=x;//储存边的信息 b[i].t=y; vis[i]=0;//初始化 a[x][num[x]]=i; num[x]++; a[y][num[y]]=i; num[y]++; } maxx=0; for(i=1;i<=n;i++)//一开始wa就是没有这个,因为可能有些点互相不连通 { if(!dis[i]) dfs(1,1); } printf("%d\n",maxx); } return 0;}
阅读全文
0 0
- POJ 3895 Cycles of Lanes(dfs+模拟)
- poj-3895-Cycles of Lanes 简单DFS
- POJ 3895 Cycles of Lanes(DFS)
- POJ 3895 Cycles of Lanes (dfs)
- POJ 3895 Cycles of Lanes(DFS)
- POJ 3895 Cycles of Lanes
- POJ 3895 Cycles of Lanes
- POJ 3895 Cycles of Lanes
- POJ 3895 Cycles of Lanes
- poj 3895 Cycles of Lanes(DFS找最大环)
- POJ3895 Cycles of Lanes,DFS
- poj 3895.Cycles of Lanes
- POJ 3895 Cycles of Lanes 已被翻译
- poj3895 Cycles of Lanes
- poj3895 Cycles of Lanes
- poj 3895 Cycles of Lanes 修改tarjan算法求图中最大环
- poj3895Cycles of Lanes(dfs)
- poj 3377 Ferry Lanes(dij+heap)
- PHP与 RESTful风格的swagger结合生成API
- 当你完不成自己的程序时,就不要睡觉......
- mybatis进阶(5)--多对多查询
- centos7.2 安装hdp2.6.1.0
- iOS10富文本推送--NotificationServiceExtension
- POJ 3895 Cycles of Lanes(dfs+模拟)
- codeforces830B Cards Sorting -- 线段树
- 前端笔记——html(1)
- php7.1.6 + redis
- Bootstrap组件系列之福利篇几款好用的组件(推荐)
- Shell系列—Shell 输入/输出重定向
- XZ_iOS之崩溃信息的查看,调用堆栈的调试技巧
- 四种途径将HTML5 web应用变成android应用
- UILabel删除线不显示