1. 程式人生 > >bzoj1023 [SHOI2008]cactus仙人掌圖 & poj3567 Cactus Reloaded——求仙人掌直徑

bzoj1023 [SHOI2008]cactus仙人掌圖 & poj3567 Cactus Reloaded——求仙人掌直徑

pri 仙人掌直徑 ios add scan tps lse reload bzoj

題目:https://www.lydsy.com/JudgeOnline/problem.php?id=1023

   http://poj.org/problem?id=3567

仙人掌!直接模仿 lyd 的代碼;

大概就是 tarjan 找環 + 單調隊列優化 dp,然後縮環成鏈繼續遞歸;

直接模仿著寫的,感覺好妙啊;

不太明白邊為什麽要開成點數的4倍。

代碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=5e4+5;
int
n,m,hd[maxn],ct,fa[maxn],f[maxn],a[maxn<<1],ans,q[maxn<<1],low[maxn],dfn[maxn],tim; struct N{ int to,nxt,w; N(int t=0,int n=0,int w=0):to(t),nxt(n),w(w) {} }ed[maxn<<2];//<<2 void add(int x,int y){ed[++ct]=N(y,hd[x]); hd[x]=ct;} void dp(int x,int y) { int tp=0,i,l,r;
for(i=y;i!=x;i=fa[i])a[++tp]=f[i]; for(i=1,a[++tp]=f[x];i<m;i++)a[i+tp]=a[i]; l=r=1;q[1]=1; int p=tp/2; for(int i=2;i<=tp+p;i++)//+p! { while(l<=r&&i-q[l]>p)l++; ans=max(ans,a[q[l]]+a[i]+i-q[l]); while(l<=r&&a[q[r]]+i-q[r]<=a[i])r--;//
<= q[++r]=i;// } for(int i=1;i<tp;i++)//< f[x]=max(f[x],a[i]+min(i,tp-i));//min!! } void tarjan(int x) { dfn[x]=low[x]=++tim; for(int i=hd[x],u;i;i=ed[i].nxt) { if((u=ed[i].to)==fa[x])continue; if(!dfn[u]) { fa[u]=x; tarjan(u); low[x]=min(low[x],low[u]); if(dfn[x]<low[u])//不在一個環上 { ans=max(ans,f[x]+f[u]+1); f[x]=max(f[x],f[u]+1); } } else low[x]=min(low[x],dfn[u]); } for(int i=hd[x],u;i;i=ed[i].nxt) if(fa[u=ed[i].to]!=x&&dfn[x]<dfn[u])dp(x,u);//&&dfn[x]<dfn[u] 則x是入環點,y是環終點 } int main() { scanf("%d%d",&n,&m); for(int i=1,x,y,k;i<=m;i++) { scanf("%d%d",&k,&x); for(int j=1;j<k;j++) { scanf("%d",&y); add(x,y); add(y,x); x=y; } } tarjan(1); printf("%d",ans); return 0; }

bzoj1023 [SHOI2008]cactus仙人掌圖 & poj3567 Cactus Reloaded——求仙人掌直徑