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

## 为什么运行错误

Posted by 826529615 at 2011-09-09 23:20:24 on Problem 3522
```type
rec = record
x, y, d: longint;
end;

var
n, m, i, now, ans, min, max: longint;
e: array[0..55010] of rec;
f: array[0..1000] of longint;

procedure qs(l, r: longint);
var
i, j, mid: longint;
t: rec;
begin
i := l;
j := r;
mid := e[(l+r) div 2].d;
repeat
while e[i].d < mid do inc(i);
while e[j].d > mid do dec(j);
if i <= j then
begin
t := e[i];
e[i] := e[j];
e[j] := t;
inc(i);
dec(j);
end;
until i > j;
if l < j then qs(l,j);
if i < r then qs(i,r);
end;

function get(k: longint):longint;
begin
if f[k] = k then exit(k);
f[k] := get(f[k]);
get := f[k];
end;

procedure kur(st: longint);
var
i, p, x, y: longint;
begin
p := 0;
for i := 1 to n do f[i] := i;
i := st;
min := e[st].d;
while (p < n-1) or (i <= m) do
begin
x := get(e[i].x);
y := get(e[i].y);
if x <> y then
begin
f[x] := y;
max := e[i].d;
inc(p);
end;
inc(i);
end;
if p <> n-1 then now := maxlongint else now := max-min;
end;

begin
while not((n = 0) and (m = 0)) do
begin
//fillchar(e,sizeof(e),0);
for i := 1 to m do
qs(1,m);
ans := maxlongint;
for i := 1 to m-n+2 do
begin
kur(i);
if now < ans then ans := now;
end;
if ans = maxlongint then writeln(-1) else writeln(ans);
end;
end.```

Followed by: