1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <cmath> #include <set> #include <map> #define mp make_pair #define pb push_back #define pii pair<int,int> #define link(x) for(edge *j=h[x];j;j=j->next) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) const int MAXN=3e5+10; const int NM=1e6+10; const double eps=1e-8; const int inf=1e9+9; #define ll long long using namespace std; struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e; void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;} ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; }
int dfn[MAXN],low[MAXN],n,m,sum[MAXN]; vector<int>p[MAXN]; vector<pii>vec[MAXN]; int st[MAXN],tot,cnt,num; int Sum[MAXN]; typedef struct node{ int x,y; }node; node que[NM]; void init(){ inc(i,1,n)sum[i]=0; for(int i=1;i<=num;i++)vec[i].clear(),dfn[i]=low[i]=0; for(int i=1;i<=num-n;i++)p[i].clear(),Sum[i]=0; } void tarjan_Bcc(int x,int pre){ dfn[x]=low[x]=++cnt; link(x){ if(dfn[j->t]&&dfn[j->t]<dfn[x]&&j->t!=pre){ low[x]=min(low[x],dfn[j->t]); st[++tot]=j->v; } else if(!dfn[j->t]){ st[++tot]=j->v;sum[j->t]=sum[x]+1; tarjan_Bcc(j->t,x); low[x]=min(low[x],low[j->t]); if(low[j->t]>=dfn[x]){ if(st[tot]==j->v)vec[x].pb(mp(j->t,1)),tot--; else{ num++;p[num-n].clear(); int y=st[tot--]; if(dfn[que[y].x]>dfn[que[y].y])p[num-n].pb(que[y].x); else p[num-n].pb(que[y].y); while(1){ y=st[tot--]; if(dfn[que[y].x]>dfn[que[y].y])p[num-n].pb(que[y].y); else p[num-n].pb(que[y].x); if(y==j->v)break; } reverse(p[num-n].begin(),p[num-n].end()); Sum[num-n]=p[num-n].size(); for(int i=1;i<p[num-n].size();i++){ int c=sum[p[num-n][i]]-sum[x]; vec[num].pb(mp(p[num-n][i],min(c,Sum[num-n]-c))); } vec[x].pb(mp(num,0)); } } } } } int maxx[MAXN],ans[MAXN]; int rt,cntk; typedef struct Node{ int l,r,maxx,maxx1; }Node; Node d[MAXN*21]; int k1,k2; void update(int &x,int l,int r,int t){ if(!x)x=++cntk,d[x].l=d[x].r=0,d[x].maxx=d[x].maxx1=-inf; if(l==r){d[x].maxx=k1-k2;d[x].maxx1=k1+k2;return ;} int mid=(l+r)>>1; if(t<=mid)update(d[x].l,l,mid,t); else update(d[x].r,mid+1,r,t); d[x].maxx=d[x].maxx1=-inf; if(d[x].l)d[x].maxx=max(d[x].maxx,d[d[x].l].maxx),d[x].maxx1=max(d[x].maxx1,d[d[x].l].maxx1); if(d[x].r)d[x].maxx=max(d[x].maxx,d[d[x].r].maxx),d[x].maxx1=max(d[x].maxx1,d[d[x].r].maxx1); } int ans1; void query1(int x,int l,int r,int ql,int qr){ if(!x)return ; if(ql<=l&&r<=qr){ans1=max(ans1,d[x].maxx);return ;} int mid=(l+r)>>1; if(ql<=mid)query1(d[x].l,l,mid,ql,qr); if(qr>mid)query1(d[x].r,mid+1,r,ql,qr); } void query2(int x,int l,int r,int ql,int qr){ if(!x)return ; if(ql<=l&&r<=qr){ans1=max(ans1,d[x].maxx1);return ;} int mid=(l+r)>>1; if(ql<=mid)query2(d[x].l,l,mid,ql,qr); if(qr>mid)query2(d[x].r,mid+1,r,ql,qr); } void dfs(int x,int pre){ for(int i=0;i<vec[x].size();i++){ if(vec[x][i].first==pre)continue; dfs(vec[x][i].first,x); maxx[x]=max(maxx[x],maxx[vec[x][i].first]+vec[x][i].second); ans[x]=max(ans[x],ans[vec[x][i].first]); } if(x<=n){ int maxx1=0,maxx2=0; for(int i=0;i<vec[x].size();i++){ if(vec[x][i].first==pre)continue; int y=maxx[vec[x][i].first]+vec[x][i].second; if(y>maxx1)maxx2=maxx1,maxx1=y; else if(y>maxx2)maxx2=y; } ans[x]=max(ans[x],maxx1+maxx2); } else{ rt=0;cntk=0;int sz=p[x-n].size(); for(int i=0;i<p[x-n].size();i++){ k1=maxx[p[x-n][i]];k2=i; update(rt,1,2*sz,i+1); k1=maxx[p[x-n][i]];k2=i+sz; update(rt,1,2*sz,1+i+sz); } for(int i=0;i<p[x-n].size();i++){ int y=sz+i+1;int t=sz/2; ans1=-inf;query1(rt,1,2*sz,y-t,y-1); ans[x]=max(ans[x],maxx[p[x-n][i]]+ans1+y-1); ans1=-inf;query1(rt,1,2*sz,y-sz+1,y-t-1); ans[x]=max(ans[x],maxx[p[x-n][i]]-y+1+ans1+sz); } } } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ memset(h,0,sizeof(h));o=e; int cnt1=0; int x,y,k; num=n; inc(i,1,m){ k=read();x=read(); inc(j,2,k){ y=read(); cnt1++;que[cnt1].x=x;que[cnt1].y=y; add(x,y,cnt1);add(y,x,cnt1); x=y; } } tarjan_Bcc(1,0); inc(i,1,num)maxx[i]=ans[i]=0; dfs(1,0); printf("%d\n",ans[1]); init(); } return 0; }
|