如果某個無向連通圖的任意一條邊至多只出現在一條簡單回路(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\n",ans); 84 return 0; 85 }