您的位置:首页 > 其它

POJ 1182食物链——并查集模板题

2011-10-08 18:02 369 查看
没什么好说的了,用CL2姐姐的话说,题解满天飞,标程也满天飞……

我的代码:

Program eat;//by_Thispoet
Const
maxn=50005;
Var
father,data			:Array[1..maxn]of Longint;
ans,i,k,n,m,p,q		:Longint;

Function Root(i:Longint):Longint;
var t:Longint;
begin
if father[i]=i then exit(i);
t:=father[i];
father[i]:=Root(father[i]);
data[i]:=(data[i]+data[t])mod 3;
exit(father[i]);
end;

BEGIN

readln(n,m);
ans:=0;
for i:=1 to n do father[i]:=i;
fillchar(data,sizeof(data),0);
while m>0 do
begin
readln(k,p,q);
if (p>n)or(q>n) then
begin
dec(m);
inc(ans);
continue;
end;//pos 1
case k of
1:
begin
if Root(p)=Root(q) then
begin
if data[p]<>data[q] then
inc(ans);
end else
begin
i:=Root(p);
father[i]:=q;
data[i]:=(3-data[p])mod 3;
end;
end;
2:
begin
if Root(p)=Root(q) then
begin
i:=(data[p]-data[q]+3)mod 3;
if i<>1 then inc(ans);
end else
begin
i:=Root(p);
father[i]:=q;
data[i]:=(4-data[p])mod 3;
end;
end;
end;
dec(m);
end;
writeln(ans);

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