程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3895 Cycles of Lanes

POJ 3895 Cycles of Lanes

編輯:C++入門知識

POJ 3895 Cycles of Lanes


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

Southeastern European Regional Programming Contest 2009

求一個無向圖裡的最大環,額,開始用的鄰連接矩陣,超內存了無數次,

只能轉向鄰接表啦=。=,話說我比賽的時候wa了4發,不能再愛了。順便學了鄰接表。

#include
#include
#include
#include
#include
using namespace std;
const int maxn=4500;
struct node{
    int to,next;
}e[maxn<<2];
int f[maxn];
int head[maxn];
int n,m,num;
int ans;
void build(int u,int v)
{
    e[num].to=v;
    e[num].next=head[u];
    head[u]=num;
    num++;
}
void dfs(int cur,int pos)
{
    if(f[cur])
    {
        if(ans
最後附上我超內存的代碼=。=引以為戒

#include
#include
#include
#include
#include
using namespace std;
const int maxn=5000;
int visit[maxn][maxn];
int mp[maxn][maxn];
int n,m,sum;
bool flag;
void dfs(int k,int s,int p)
{
    if(k==p&&s)
    {
        if(s>sum)
            sum=s;
        flag=true;
    }
    if(k==p&&s<=sum&&s)
    {
        flag=true;
        return ;
    }
    if(flag)
        return ;
    for(int i=1;i<=n;i++)
    {
        if(mp[k][i]&&!visit[k][i])
        {
            visit[k][i]=visit[i][k]=1;
            dfs(i,s+1,p);
            visit[k][i]=visit[i][k]=0;
        }
    }
}
int main()
{
    int t,u,v;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(mp,0,sizeof(mp));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            mp[u][v]=mp[v][u]=1;
        }
        sum=0;
        for(int i=1;i<=n;i++)
        {
            memset(visit,0,sizeof(visit));
            flag=false;
            dfs(i,0,i);
        }
        printf("%d\n",sum);
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved