poj3895 Cycles of Lanes

48 篇文章 0 订阅

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
其实,这一题,要注意,图一定是连通的,一定有圈,一个边只在一个环内,这就很简单了,只要一次深搜,就可以了,刚开始想复杂了,以为,一个点,可能会有多个圈,所以tl了好多次,题意要理解清楚啊!用一个步长数组,记录每个点,第一个经过的步数,然后,第二次经过的时候,就可以重新最小值了,因为,能第二次到,其实,就是一个环!很水啊,有木有,当然怎么就没想到了,当时怎么就,把题读错了呢?
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct lane{
    int val ,path,used;
};
int minxx,visit[50000],steplong[5000];
vector<lane > p[5000];
void dfs(int start,int current,int step )
{
    int i;
    for(i=0;i<p[current].size();i++)
    {
        if(!visit[p[current][i].path])
        {
            visit[p[current][i].path]=1;
           
            if(steplong[p[current][i].val]==-1)//第一次结过这个点
            {
                steplong[p[current][i].val]=step;
               
                dfs(start,p[current][i].val,step+1);
            }

            else
            {
                if(step-steplong[p[current][i].val]>minxx)
                    minxx=step-steplong[p[current][i].val];


            }
            visit[p[current][i].path]=0;//标记恢复
        }
    }
}
int main()
{
    int t,i,n,m,s,e,allpath;
    lane temp;
    scanf("%d",&t);
    while(t--)
    {
        allpath=0;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(i=1;i<=n;i++)
            {
                p[i].clear();
            }
            for(i=1;i<=m;i++)
            {
                scanf("%d%d",&s,&e);
                temp.val=s;
                temp.path=allpath;//标记为未访问
                temp.used=0;
                p[e].push_back(temp);//建立双向表
                temp.val=e;
                p[s].push_back(temp);
                visit[allpath]=0;//标记为未访问
                allpath++;

            }
            minxx=0;//标记为最小值
            memset(steplong,-1,sizeof(steplong));
            steplong[1]=0;//第一步为0
            dfs(1,1,1);
            printf("%d\n",minxx);
        }
    }
    return 0;
}



FAQ | About Virtual Judge |  Forum |  Discuss |  Open Source Project
All Copyright Reserved ©2010-2012  HUST ACM/ICPC TEAM 
Anything about the OJ, please ask in the  forum, or contact author: Isun
Server Time: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值