博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3895 Cycles of Lanes(DFS)
阅读量:5332 次
发布时间:2019-06-14

本文共 2577 字,大约阅读时间需要 8 分钟。

Cycles of Lanes
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 1062
Accepted: 392

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存

#include
#include
#include
#include
using namespace std;const int N = 4500;vector
ma[N];int vis[N], a, b, cas, n, m, ans;int dfs (int i, int no){ vis[i] = no; for (int j = 0; j < ma[i].size(); ++j) { if (!vis[ma[i][j]]) dfs (ma[i][j], no + 1); else ans = max (ans, no - vis[ma[i][j]] + 1); }}int main(){ scanf ("%d", &cas); while (cas--) { scanf ("%d%d", &n, &m); for (int i = 1; i <= n; ++i) ma[i].clear(); for (int i = 1; i <= m; ++i) { scanf ("%d%d", &a, &b); ma[a].push_back (b); ma[b].push_back (a); } ans = 0; memset (vis, 0, sizeof (vis)); for (int i = 1; i <= n; ++i) if (!vis[i]) dfs (i, 1); if (ans >= 3) printf ("%d\n", ans); else printf ("0\n"); } return 0;}

转载于:https://www.cnblogs.com/acvay/p/3947319.html

你可能感兴趣的文章
mysql统计一张表中条目个数的方法
查看>>
ArcGIS多面体(multipatch)解析——引
查看>>
css3渐变画斜线 demo
查看>>
JS性能DOM优化
查看>>
设计模式 单例模式 使用模板及智能指针
查看>>
c#的const可以用于引用类型吗
查看>>
手动实现二值化
查看>>
What Linux bind mounts are really doing
查看>>
linux top命令详解
查看>>
博弈论小结
查看>>
模拟Post登陆带验证码的网站
查看>>
NYOJ458 - 小光棍数
查看>>
java中常用方法
查看>>
【Programming Clip】06、07年清华计算机考研上机试题解答(个别测试用例无法通过)...
查看>>
canvas动画
查看>>
4,7周围玩家
查看>>
关于webpack升级过后不能打包的问题;
查看>>
vue - 生命周期
查看>>
SQL Server用户权限详解
查看>>
Python正则表达式
查看>>