| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
另一种解法?本人是刚学并查集的菜鸟,在网上看到一份本题的代码,感觉好像跟判断深度差的奇偶性的方法不同,不理解是什么意思,哪位大牛可以帮忙解释一下,感激不尽
代码如下
program pku1703;
var
t,n,m:longint;
fa:array[0..200005]of longint;
function getfa(x:longint):longint;
begin
if x=fa[x] then getfa:=x
else
begin
fa[x]:=getfa(fa[x]);
getfa:=fa[x];
end;
end;
procedure union(x,y:longint);
var
f1,f2:longint;
begin
f1:=getfa(x);
f2:=getfa(y);
fa[f1]:=f2;
end;
procedure work;
var
ch:char;
i,k,x,y:longint;
begin
readln(t);
for k:=1 to t do
begin
fillchar(fa,sizeof(fa),0);
readln(n,m);
for i:=1 to 2*n do
fa[i]:=i;
for i:=1 to m do
begin
read(ch);
case ch of
'D':begin
readln(x,y);
union(x,y+n);
union(x+n,y);
end;
'A':begin
readln(x,y);
if getfa(x)=getfa(y)then writeln('In the same gang.')
else
begin
if getfa(x)=getfa(y+n)then writeln('In different gangs.')
else writeln('Not sure yet.');
end;
end;
end;
end;
end;
end;
begin
assign(input,'pku1703.in');
assign(output,'pku1703.out');
reset(input);
rewrite(output);
work;
close(input);
close(output);
end.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator