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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
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<pos-f[cur])
            ans=pos-f[cur];
        return ;
    }
    f[cur]=pos;
    for(int i=head[cur];i!=-1;i=e[i].next)//记录的是cur(起点)到终点的边的集合
        dfs(e[i].to,pos+1);
}
int main()
{
    int t,u,v;
    scanf("%d",&t);
    while(t--)
    {
        num=0;
        memset(head,-1,sizeof(head));
        memset(f,0,sizeof(f));
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d",&u,&v);
            build(u,v);
            build(v,u);
        }
         ans=0;
         dfs(1,1);
         if(ans<3)
            ans=0;
        printf("%d\n",ans);
    }
    return 0;
}

最后附上我超内存的代码=。=引以为戒

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
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;
}

POJ 3895 Cycles of Lanes,布布扣,bubuko.com

时间: 2024-10-10 03:48:28

POJ 3895 Cycles of Lanes的相关文章

POJ 3895 Cycles of Lanes (dfs)

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

poj 3895 Cycles of Lanes 修改tarjan算法求图中最大环

题意: 给一个边权均为1的无向图,求图中的最大环. 分析: tarjan算法一般用来强连通分量,它依次访问图中的各个强连通分量,这题要求最大环,而环也是强连通分量的一部分,所以可以在每个点访问其他点时修改时间戳,达到每个环上时间戳连续的目的,这样当访问到一个栈中节点时就能直接更新最大环了.根据同样的思路,即使边权任意,也可求最大环或最小环. 代码: //poj 3895 //sep9 #include <iostream> #include <vector> #include &l

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 1422 Air Raid

题目链接: http://poj.org/problem?id=1422 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can

POJ 2378 树状dp

Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3849   Accepted: 2304 Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible

POJ - 1006 Biorhythms (中国剩余定理)

Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respec

poj 2377 Bad Cowtractors

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10194   Accepted: 4333 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N

poj 1422 Air Raid (二分匹配)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6520   Accepted: 3877 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,