bzoj 1529 并查集

来源:互联网 发布:mac大型游戏排行榜 编辑:程序博客网 时间:2024/04/27 16:39

题意:n个存钱罐,每个的钥匙都在一个存钱罐里,想要把所有存钱罐中的钱都拿出,问最少砸几个存钱罐

一眼看过去就是找tarjan缩点后重建的图中有几个入度为0的强连通块...mdzz爆栈了

然后才发现,其实就是找连通块的个数,mdzz并查集不就行了(思想类似团伙)

var        n,ans,x,y       :longint;        i               :longint;        f               :array[0..1000010] of longint;function get_father(x:longint):longint;begin   if x=f[x] then exit(x);   f[x]:=get_father(f[x]);   exit(f[x]); end;begin   read(n);   for i:=1 to n do f[i]:=i;   for i:=1 to n do   begin      read(x);      x:=get_father(x);      y:=get_father(i);      if x<>y then f[y]:=x;   end;   ans:=0;   for i:=1 to n do if f[i]=i then inc(ans);   writeln(ans);end.
——by Eirlys

0 0
原创粉丝点击