# bzoj 1638 dfs

pascal同志记得开Q-（也可能是本蒟蒻写丑了）

{\$Q-}
type
rec=record
a,b:longint;
end;

var
n,m,ll          :longint;
last,f,g        :array[0..5010] of longint;
pre,other       :array[0..50010] of longint;
i               :longint;
ans             :longint;
l               :array[0..50010] of rec;
procedure connect(x,y:longint);
begin
inc(ll);
pre[ll]:=last[x];
last[x]:=ll;
other[ll]:=y;
end;

procedure dfs1(x:longint);
var
p,q:longint;
begin
q:=last[x];
if q=0 then
begin
f[x]:=1; exit;
end;
while (q<>0) do
begin
p:=other[q];
if f[p]=0 then dfs1(p);
inc(f[x],f[p]);
q:=pre[q];
end;
end;

procedure dfs2(x:longint);
var
p,q:longint;
begin
q:=last[x];
if q=0 then
begin
g[x]:=1; exit;
end;
while (q<>0) do
begin
p:=other[q];
if g[p]=0 then dfs2(p);
inc(g[x],g[p]);
q:=pre[q];
end;
end;

begin
for i:=1 to m do read(l[i].a,l[i].b);
for i:=1 to m do connect(l[i].b,l[i].a);
dfs1(n);
ll:=0;
for i:=1 to n do last[i]:=0;
for i:=1 to m do connect(l[i].a,l[i].b);
for i:=1 to n do if g[i]=0 then dfs2(i);
ans:=0;
for i:=1 to m do
if (f[l[i].a]*g[l[i].b]>ans) then ans:=f[l[i].a]*g[l[i].b];
writeln(ans);
end.

——by Eirlys

10-22 236
04-12 213
11-24 135
11-19 666
07-10 566
10-31 330
10-25 257
01-12 233
09-19 318
05-02 192
11-09 273
11-26 81
05-31 807
09-18 444
04-01 406