bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)
Description
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意
两点u,v,存在一条u到v的有向路径或者从v到u的有向路径。若G'=(V',E')满足V'?V,E'是E中所有跟V'有关的边,
则称G'是G的一个导出子图。若G'是G的导出子图,且G'半连通,则称G'为G的半连通子图。若G'是G所有半连通子图
中包含节点数最多的,则称G'是G的最大半连通子图。给定一个有向图G,请求出G的最大半连通子图拥有的节点数K
,以及不同的最大半连通子图的数目C。由于C可能比较大,仅要求输出C对X的余数。
Input
第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述接下来M行,每行两个正整
数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。N ≤1
00000, M ≤1000000;对于100%的数据, X ≤10^8
Output
应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.
Sample Input
6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4
1 2
2 1
1 3
2 4
5 6
6 4
Sample Output
3
3
3
先用tarjan将强连通分量缩点,然后答案就是最长路,拓扑排序+dp就可,要求方案总数,只要在dp的时候搞一搞,记录方案数就好了。
我是参考hzwer大佬的,在此表示感谢。
1 program semi(input,output); 2 type 3 etype=record 4 t,next:longint; 5 end; 6 var 7 e,c:array[0..1000010]of etype; 8 last,dfn,low,q,hav,belong,r,a,f,g,vis:array[0..100010]of longint; 9 inq:array[0..100010]of boolean; 10 n,m,p,i,j,u,v,cnt,tot,top,h,t,max,ans:longint; 11 procedure add(u,v:longint); 12 begin 13 inc(cnt);e[cnt].t:=v;e[cnt].next:=last[u];last[u]:=cnt; 14 end; 15 procedure tarjan(k:longint); 16 var 17 i:longint; 18 begin 19 inc(cnt);dfn[k]:=cnt;low[k]:=cnt; 20 inc(top);q[top]:=k;inq[k]:=true; 21 i:=last[k]; 22 while i<>0 do 23 begin 24 if dfn[e[i].t]=0 then begin tarjan(e[i].t);if low[e[i].t]<low[k] then low[k]:=low[e[i].t]; end 25 else if inq[e[i].t] and (dfn[e[i].t]<low[k]) then low[k]:=dfn[e[i].t]; 26 i:=e[i].next; 27 end; 28 if low[k]=dfn[k] then 29 begin 30 inc(tot);hav[tot]:=0; 31 while q[top]<>k do begin inq[q[top]]:=false;belong[q[top]]:=tot;inc(hav[tot]);dec(top); end; 32 dec(top);inq[k]:=false;belong[k]:=tot;inc(hav[tot]); 33 end; 34 end; 35 procedure ins(u,v:longint); 36 begin 37 inc(cnt);c[cnt].t:=v;c[cnt].next:=a[u];a[u]:=cnt;inc(r[v]); 38 end; 39 begin 40 assign(input,'semi.in');assign(output,'semi.out');reset(input);rewrite(output); 41 readln(n,m,p); 42 cnt:=0;fillchar(last,sizeof(last),0); 43 for i:=1 to m do begin readln(u,v);add(u,v); end; 44 fillchar(dfn,sizeof(dfn),0);tot:=0; 45 for i:=1 to n do if dfn[i]=0 then begin cnt:=0;top:=0;tarjan(i); end; 46 cnt:=0;fillchar(a,sizeof(a),0);fillchar(r,sizeof(r),0); 47 for i:=1 to n do 48 begin 49 j:=last[i]; 50 while j<>0 do 51 begin 52 if belong[i]<>belong[e[j].t] then ins(belong[i],belong[e[j].t]); 53 j:=e[j].next; 54 end; 55 end; 56 h:=0;t:=0; 57 for i:=1 to tot do 58 begin 59 if r[i]=0 then begin inc(t);q[t]:=i; end; 60 f[i]:=hav[i];g[i]:=1; 61 end; 62 fillchar(vis,sizeof(vis),0); 63 while h<t do 64 begin 65 inc(h);i:=a[q[h]]; 66 while i<>0 do 67 begin 68 dec(r[c[i].t]);if r[c[i].t]=0 then begin inc(t);q[t]:=c[i].t; end; 69 if vis[c[i].t]<>q[h] then 70 begin 71 if f[q[h]]+hav[c[i].t]>f[c[i].t] then begin f[c[i].t]:=f[q[h]]+hav[c[i].t];g[c[i].t]:=g[q[h]]; end 72 else if f[q[h]]+hav[c[i].t]=f[c[i].t] then g[c[i].t]:=(g[c[i].t]+g[q[h]]) mod p; 73 vis[c[i].t]:=q[h]; 74 end; 75 i:=c[i].next; 76 end; 77 end; 78 max:=0; 79 for i:=1 to tot do if f[i]>max then begin max:=f[i];ans:=g[i]; end else if f[i]=max then ans:=(ans+g[i]) mod p; 80 writeln(max);writeln(ans); 81 close(input);close(output); 82 end.