讨论 / 飘逸的小代码
472549025 2011-10-25 19:17:00
点我顶贴 收藏 删除
var

n,k,ans:longint;

father,g:array[1..50000] of longint;

function find(x:longint):longint;

var

i:longint;

begin

if (father[x]=x) then exit(x);

i:=father[x];

father[x]:=find(father[x]);

g[x]:=(g[x]+g[i]) mod 3;

exit(father[x]);

end;

procedure union(x,y,d:longint);

var

tx:longint;

begin

tx:=find(x);

g[tx]:=(-g[x]-(d-1)+3) mod 3;

father[tx]:=y;

end;

procedure work;

var

d,x,y,i:longint;

begin

readln(n,k);

for i:=1 to n do

begin

father[i]:=i;

g[i]:=0;

end;

ans:=0;

for i:=1 to k do

begin

readln(d,x,y);

if (x>n) or (y>n) then

begin

inc(ans);

continue;

end;

if (d=2) and (x=y)then

begin

inc(ans);

continue;

end;

if d=1 then

begin

if find(x)=find(y) then

begin

if g[x]<>g[y] then inc(ans);

end

else

begin

union(x,y,d);

end;

end;

if d=2 then

begin

if find(x)=find(y) then

begin

if ((g[x]+1) mod 3)<>g[y] then inc(ans);

end

else

begin

union(x,y,d);

end;

end;

end;

writeln(ans);

end;

begin

work;

end.

查看更多回复
提交回复