bzoj 1638: [Usaco2007 Mar]Cow Traffic

      略坑啊,题目保证答案小于maxlongint,结果我开int64才过。题目描述很含糊。大概是求某一条边最多能同时出现在几条路径上。两次DP解决,而且题目说“每一条道路都由编号较小的路口通向编号较大的路口”,所以不用拓扑排序。

  1: program bzoj1638;
  2: uses math;
  3: const maxn=50000;maxm=500000;
  4: var u,tot,n,m,i,a,b:longint;
  5:     ans:int64;
  6:     v,next:array[0..maxm*2] of longint;
  7:     Pos,Neg:array[0..maxn] of longint;
  8:     f,g:array[0..maxn] of int64;
  9: procedure build(var head:array of longint;a,b:longint);
 10:           begin
 11:           inc(tot);
 12:           v[tot]:=b;
 13:           next[tot]:=head[a];
 14:           head[a]:=tot;
 15:           end;
 16: begin
 17: readln(n,m);g[n]:=1;ans:=0;
 18: for i:=1 to m do
 19:   begin
 20:   readln(a,b);
 21:   build(pos,a,b);
 22:   build(neg,b,a);
 23:   end;
 24: for u:=1 to n do
 25:   begin i:=Neg[u];
 26:   if i=0 then f[u]:=1;
 27:   while i<>0 do begin
 28:     inc(f[u],f[v[i]]);
 29:     i:=next[i];end;
 30:   end;
 31: for u:=n-1 downto 1 do
 32:   begin i:=Pos[u];
 33:   while i<>0 do begin
 34:     inc(g[u],g[v[i]]);
 35:     i:=next[i];end;
 36:   end;
 37: for u:=1 to n-1 do
 38:   begin i:=pos[u];
 39:   while i<>0 do begin
 40:     ans:=max(f[u]*g[v[i]],ans);
 41:     i:=next[i];end;
 42:   end;
 43: writeln(ans);
 44: end.
 45: 
posted @ 2012-11-01 17:09  ljlin  阅读(249)  评论(0编辑  收藏  举报