zoukankan      html  css  js  c++  java
  • 图论:仙人掌图-直径

    如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人图(cactus)。所谓简单回路就是指在图上不重复经过任何一个顶点的回路。

    输入的第一行包括两个整数n和m(1≤n≤50000以及0≤m≤10000)。其中n代表顶点个数,我们约定图中的顶点将从1到n编号。接下来一共有m行。代表m条路径。

    每行的开始有一个整数k(2≤k≤1000),代表在这条路径上的顶点个数。接下来是k个1到n之间的整数,分别对应了一个顶点,相邻的顶点表示存在一条连接这两个顶点的边。

    对不起直接贴了,以后补,此博客暂时作为代码仓库,如侵删

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstring>
     7 #define inf 1000000000
     8 #define ll long long 
     9 using namespace std;
    10 inline int read()
    11 {
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    14     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 int n,m,cnt,ind,ans;
    18 int last[50005],deep[50005],f[50005];
    19 int low[50005],dfn[50005],fa[50005];
    20 int a[100005],q[100005],l,r;
    21 struct edge{int to,next;}e[20000005];
    22 void insert(int u,int v)
    23 {
    24     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
    25     e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
    26 }
    27 void dp(int root,int x)
    28 {
    29     int tot=deep[x]-deep[root]+1;
    30     for(int i=x;i!=root;i=fa[i])
    31         a[tot--]=f[i];
    32     a[tot]=f[root];
    33     tot=deep[x]-deep[root]+1;
    34     for(int i=1;i<=tot;i++)a[i+tot]=a[i];
    35     q[1]=1;l=r=1;
    36     for(int i=2;i<=2*tot;i++)
    37     {
    38         while(l<=r&&i-q[l]>tot/2)l++;
    39         ans=max(ans,a[i]+i+a[q[l]]-q[l]);
    40         while(l<=r&&a[q[r]]-q[r]<=a[i]-i)r--;
    41         q[++r]=i;
    42     }
    43     for(int i=2;i<=tot;i++)
    44         f[root]=max(f[root],a[i]+min(i-1,tot-i+1));
    45 }
    46 void dfs(int x)
    47 {
    48     low[x]=dfn[x]=++ind;
    49     for(int i=last[x];i;i=e[i].next)
    50         if(e[i].to!=fa[x])
    51         {
    52             if(!dfn[e[i].to])
    53             {
    54                 fa[e[i].to]=x;
    55                 deep[e[i].to]=deep[x]+1;
    56                 dfs(e[i].to);
    57                 low[x]=min(low[x],low[e[i].to]);
    58             }
    59             else low[x]=min(low[x],dfn[e[i].to]);
    60             if(dfn[x]<low[e[i].to])
    61             {
    62                 ans=max(ans,f[x]+f[e[i].to]+1);
    63                 f[x]=max(f[x],f[e[i].to]+1);
    64             }
    65         }
    66     for(int i=last[x];i;i=e[i].next)
    67         if(fa[e[i].to]!=x&&dfn[x]<dfn[e[i].to])
    68             dp(x,e[i].to);
    69 }
    70 int main()
    71 {
    72     n=read();m=read();
    73     for(int i=1;i<=m;i++)
    74     {
    75         int k=read(),a=read();
    76         for(int i=2;i<=k;i++)
    77         {
    78             int b=read();
    79             insert(a,b);a=b;
    80         }
    81     }
    82     dfs(1);
    83     printf("%d
    ",ans);
    84     return 0;
    85 }
  • 相关阅读:
    hdu 5146 Sequence
    hdu 1232 畅通工程
    hdu 1213 How Many Tables
    hdu 2822 Dogs
    hdu 1242 Rescue
    hdu 5101 Select
    hdu 1873 看病要排队
    hdu 5112 A Curious Matt
    hdu 5154 Harry and Magical Computer
    hdu 1548 A strange lift
  • 原文地址:https://www.cnblogs.com/aininot260/p/9623954.html
Copyright © 2011-2022 走看看