NOIP2010 关押罪犯 (并查集)

若x,y有关系 将x与y的补集, y与x的补集建立关系

const maxn=;
maxm=;
var eg:array[..maxm,..] of longint;
f:array[..maxn*] of longint;
i,j,m,n,x,y,z:longint;
procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;a:=b;b:=c;
end;
function find(x:longint):longint;
begin
if f[x]=x then exit(x);
f[x]:=find(f[x]);
exit(f[x]);
end;
procedure sort(l,r:longint);
var i,j,x:longint;
begin
i:=l; j:=r;
x:=eg[(l+r) div ,];
while i<=j do
begin
while eg[i,]>x do inc(i);
while x>eg[j,] do dec(j);
if i<=j then
begin
swap(eg[i,],eg[j,]);
swap(eg[i,],eg[j,]);
swap(eg[i,],eg[j,]);
inc(i);
dec(j);
end;
end;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
begin
readln(n,m);
for i:= to m do read(eg[i,],eg[i,],eg[i,]);
for i:= to n* do f[i]:=i;
sort(,m);
for i:= to m do
begin
x:=find(eg[i,]);
y:=find(eg[i,]);
if x=y then
begin
writeln(eg[i,]);
exit;
end;
f[x]:=find(eg[i,]+n);
f[y]:=find(eg[i,]+n);
end;
writeln();
end.
上一篇:NOIP2010关押罪犯


下一篇:GPU体系架构(一):数据的并行处理