Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 930 | Accepted: 347 |
Description
Input
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>
using namespace std;
const int N = 4500;
struct edge
{
int to,next;
}lcm[N<<6];
int flag[N];
int head[N];
int n,m,num;
int ans;
void build(int u,int v)
{
lcm[num].to = v;
lcm[num].next = head[u];
head[u] = num;
num ++;
}
void dfs(int cur,int dp)
{
int i;
if(flag[cur])
{
if(ans < dp - flag[cur])
ans = dp - flag[cur];
//flag[cur] = dp;
return;
}
flag[cur] = dp;
for(i = head[cur];i != -1;i = lcm[i].next)
{
dfs(lcm[i].to,dp + 1);
}
}
int main()
{
int i,t;
int a,b;
scanf("%d",&t);
while(t --)
{
num = 0;
memset(head,-1,sizeof(head));
memset(flag,0,sizeof(flag));
scanf("%d%d",&n,&m);
while(m --)
{
scanf("%d%d",&a,&b);
build(a,b);
build(b,a);
}
ans = 0;
dfs(1,1);
if(ans < 3)
ans = 0;
printf("%d\n",ans);
}
return 0;
}
//276K 16MS