Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

另一种解法?

Posted by hyq3235356 at 2009-11-09 13:24:17 on Problem 1703
本人是刚学并查集的菜鸟,在网上看到一份本题的代码,感觉好像跟判断深度差的奇偶性的方法不同,不理解是什么意思,哪位大牛可以帮忙解释一下,感激不尽
代码如下

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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator