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

Re:可以路径压缩,只不过需要记录到根节点距离即可

Posted by lydliyudong at 2011-05-05 13:25:43 on Problem 1988 and last updated at 2011-05-05 13:26:27
In Reply To:Re:路径压缩了,反而潮湿了。咋回事??? Posted by:qq976078267 at 2011-04-27 21:12:55
可以路径压缩,只不过需要记录到根节点距离即可
var
 fa,v,d:array[0..30000]of longint;
 n,m,i,j,x,y,a,b:longint;
 ch:char;
function find(x:longint):longint;
 var
  y:longint;
 begin
  if x=fa[x] then exit(x);
  y:=fa[x];
  fa[x]:=find(fa[x]);//路径压缩
  inc(d[x],d[y]);//记录到根节点距离
  exit(fa[x]);
 end;
begin
 n:=30000;
 for i:=1 to n do fa[i]:=i;
 filldword(v,sizeof(v)shr 2,1);
 readln(m);
 for i:=1 to m do
  begin
   read(ch);
   case ch of
    'M':
     begin
      readln(x,y);
      a:=find(x);
      b:=find(y);
      fa[a]:=b;
      d[a]:=v[b];
      inc(v[b],v[a]);
     end;
    'C':
     begin
      readln(x);
      find(x);
      writeln(d[x]);
     end;
   end;//case
  end;
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