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 |
Re:可以路径压缩,只不过需要记录到根节点距离即可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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator