您的位置:首页 > 其它

BZOJ1093: [ZJOI2007]最大半连通子图

2014-08-11 14:32 204 查看

1093: [ZJOI2007]最大半连通子图

Time Limit: 30 Sec Memory Limit: 162 MB
Submit: 1530 Solved: 589
[Submit][Status]

Description

const maxn=110000+1000;maxm=1100000+1000;
type node=record
from,go,next:longint;
end;
var e:array[0..maxm] of node;
e2:array[0..2*maxm] of node;
a,b:array[0..maxm] of longint;
n,m,h,t,x,y,i,ti,top,cnt,ans1,ans2,tot,p:longint;
q,head,head2,dfn,low,sta,scc,s,v,num,inp,vis:array[0..maxn] of longint;
function min(x,y:longint):longint;
begin
if x<y then exit(x) else exit(y);
end;

procedure insert(x,y:longint);
begin
inc(tot);
e[tot].go:=y;e[tot].next:=head[x];head[x]:=tot;
end;
procedure insert2(x,y:longint);
begin
inc(tot);
e2[tot].go:=y;e2[tot].next:=head2[x];head2[x]:=tot;
end;
procedure dfs(x:longint);
var i,y,z:longint;
begin
inc(ti);dfn[x]:=ti;low[x]:=ti;inc(top);sta[top]:=x;
i:=head[x];
while i<>0 do
begin
y:=e[i].go;
if dfn[y]=0 then
begin
dfs(y);
low[x]:=min(low[x],low[y]);
end
else if scc[y]=0 then low[x]:=min(low[x],dfn[y]);
i:=e[i].next;
end;
if low[x]=dfn[x] then
begin
inc(cnt);
while true do
begin
z:=sta[top];dec(top);
scc[z]:=cnt;inc(s[cnt]);
if z=x then break;
end;
end;
end;
procedure tarjan;
begin
ti:=0;cnt:=0;ti:=0;
fillchar(dfn,sizeof(dfn),0);
for i:=1 to n do if dfn[i]=0 then dfs(i);
end;
procedure init;
begin
readln(n,m,p);
for i:=1 to m do
begin
readln(a[i],b[i]);
insert(a[i],b[i]);
end;
end;
procedure main;
begin
tarjan;
fillchar(inp,sizeof(inp),0);tot:=0;
for i:=1 to m do
begin
x:=scc[a[i]];y:=scc[b[i]];
if x<>y then begin inc(inp[y]);insert2(x,y);end;
end;
for i:=1 to cnt do insert2(i,cnt+1);
inp[cnt+1]:=cnt;s[cnt+1]:=0;
h:=0;t:=0;
for i:=1 to cnt do
if inp[i]=0 then begin inc(t);q[t]:=i;v[i]:=s[i];num[i]:=1;end;
while h<t do
begin
inc(h);x:=q[h];
i:=head2[x];
while i<>0 do
begin
y:=e2[i].go;
if vis[y]<>x then
begin
vis[y]:=x;
if v[x]+s[y]>v[y] then begin v[y]:=v[x]+s[y];num[y]:=num[x];end
else if v[x]+s[y]=v[y] then num[y]:=(num[y]+num[x]) mod p;
end;
dec(inp[y]);if inp[y]=0 then begin inc(t);q[t]:=y;end;
i:=e2[i].next;
end;
end;
writeln(v[cnt+1]);
writeln(num[cnt+1]);
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
main;
close(input);close(output);
end.


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: