- 关押罪犯
- 2014-10-13 16:30:44 @
type node=record
x,y,v:longint;
end;
var n,m,i,fx,fy:longint;
a:array[1..100000]of node;
fa,enemy:array[1..20000]of longint;
procedure qsore(l,r:longint);
var i,j,mid:longint;t:node;
begin
i:=l;j:=r;mid:=a[(l+r) div 2].v;
repeat
while a[i].v>mid do inc(i);
while a[j].v<mid do dec(j);
if i<=j then
begin
t:=a[i];a[i]:=a[j];a[j]:=t;
inc(i);dec(j);
end;
until i>j;
if i<r then qsore(i,r);
if l<j then qsore(l,j);
end;
function getfa(x:longint):longint;
begin
if fa[x]=x then exit(x);
fa[x]:=getfa(fa[x]);
getfa:=fa[x];
end;
procedure union(x,y:longint);
var fx,fy:longint;
begin
fx:=getfa(x);
fy:=getfa(y);
fa[fx]:=fy;
end;
begin
readln(n,m);
for i:=1 to m do readln(a[i].x,a[i].y,a[i].v);
qsore(1,m);
for i:=1 to n do fa[i]:=i;
for i:=1 to m do
begin
fx:=getfa(fa[a[i].x]);
fy:=getfa(fa[a[i].y]);
if fx=fy then//已经不能再分
begin
write(a[i].v);halt;
end;
if enemy[a[i].x]=0 then enemy[a[i].x]:=a[i].y;
if enemy[a[i].y]=0 then enemy[a[i].y]:=a[i].x;
union(enemy[a[i].x],a[i].y);
union(enemy[a[i].y],a[i].x);//建立敌对阵营,敌人的敌人就是朋友的原则,因为只有两个阵营
end;
write('0');
end.
快排怒气值,怒气值越大的越早分开,直到不能分开为止
1 条评论
-
zcq784951623 LV 8 @ 2015-11-06 10:50:35
block code
type node=record
x,y,v:longint;
end;
var n,m,i,fx,fy:longint;
a:array[1..100000]of node;
fa,enemy:array[1..20000]of longint;
procedure qsore(l,r:longint);
var i,j,mid:longint;t:node;
begin
i:=l;j:=r;mid:=a[(l+r) div 2].v;
repeat
while a[i].v>mid do inc(i);
while a[j].v<mid do dec(j); if i<=j then begin t:=a[i];a[i]:=a[j];a[j]:=t; inc(i);dec(j); end; until i>j;
if i<r then qsore(i,r);
if l<j then qsore(l,j);
end;
function getfa(x:longint):longint;
begin
if fa[x]=x then exit(x);
fa[x]:=getfa(fa[x]);
getfa:=fa[x];
end;
procedure union(x,y:longint);
var fx,fy:longint;
begin
fx:=getfa(x);
fy:=getfa(y);
fa[fx]:=fy;
end;
begin
readln(n,m);
for i:=1 to m do readln(a[i].x,a[i].y,a[i].v);
qsore(1,m);
for i:=1 to n do fa[i]:=i;
for i:=1 to m do
begin
fx:=getfa(fa[a[i].x]);
fy:=getfa(fa[a[i].y]);
if fx=fy then//已经不能再分
begin
write(a[i].v);halt;
end;
if enemy[a[i].x]=0 then enemy[a[i].x]:=a[i].y;
if enemy[a[i].y]=0 then enemy[a[i].y]:=a[i].x;
union(enemy[a[i].x],a[i].y);
union(enemy[a[i].y],a[i].x);//建立敌对阵营,敌人的敌人就是朋友的原则,因为只有两个阵营
end;
write('0');
end.
- 1