bzoj 1123 tarjan+乘法原理

来源:互联网 发布:编程语言有多少 编辑:程序博客网 时间:2024/06/04 19:02

题意:n个点,m条双向边,问删除每个点后,对于有序数对(x,y)满足x,y互不连通的数对数(即(1,2)与(2,1)算2对)。其中,被删掉的点也应被统计。

题意明白以后,一眼看过去就是tarjan

因为要求统计被删除的点,所以每个点的基础答案为 (n-1)*2

如果删去的点不是割点,则它除了基础答案外不会再增加新的不连通的数对

如果删去的点是割点,那么会裂成几个连通块,统计每个连通块的大小,用乘法原理计算新的不连通的数对数(注意乘2)

var        n,m,time,x,y,l  :longint;        i               :longint;        vis             :array[0..100010] of boolean;        ans             :array[0..100010] of int64;        size            :array[0..100010] of longint;        last,low,dfn    :array[0..100010] of longint;        pre,other       :array[0..1000010] of longint;function min(a,b:longint):longint;begin   if a<b then exit(a) else exit(b);end;procedure connect(x,y:longint);begin   inc(l);   pre[l]:=last[x];   last[x]:=l;   other[l]:=y;end;procedure dfs(x:longint);var        p,q:longint;        tt:int64;begin   inc(time);   low[x]:=time;   dfn[x]:=time;   size[x]:=1;   tt:=0;   vis[x]:=true;   //   q:=last[x];   while (q<>0) do   begin      p:=other[q];      if dfn[p]=0 then      begin         dfs(p);         low[x]:=min(low[x],low[p]);         inc(size[x],size[p]);         if low[p]>=dfn[x] then         begin            inc(ans[x],tt*int64(size[p])*int64(2));            inc(tt,int64(size[p]));         end;      end else      if vis[p] then low[x]:=min(low[x],dfn[p]);      q:=pre[q];   end;   inc(ans[x],tt*int64(n-tt-1)*int64(2));end;begin   read(n,m);   for i:=1 to m do   begin      read(x,y);      connect(x,y);      connect(y,x);   end;   for i:=1 to n do ans[i]:= (n-1)*2;   for i:=1 to n do if dfn[i]=0 then dfs(i);   for i:=1 to n do writeln(ans[i]);end.
——by Eirlys


0 0
原创粉丝点击