比赛 |
20120708 |
评测结果 |
WWWWWWWWWW |
题目名称 |
最小最大生成树 |
最终得分 |
0 |
用户昵称 |
fuhao |
运行时间 |
0.815 s |
代码语言 |
Pascal |
内存使用 |
6.72 MiB |
提交时间 |
2012-07-08 09:26:59 |
显示代码纯文本
const maxn=20001;maxm=200001;
var
n,m,i,j,ans:longint;
fa:array[0..maxn] of longint;
ff,f:array[0..maxm,1..4] of longint;
intree,delete:array[0..maxm] of boolean;
function find(k:longint):longint;
begin
if fa[k]=k then exit(k) else fa[k]:=find(fa[k]);
find:=fa[k];
end;
procedure change(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end;
procedure kp1(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=f[(l+r) shr 1,3];
repeat
while f[i,3]<mid do inc(i);
while f[j,3]>mid do dec(j);
if i<=j then
begin
change(f[i,1],f[j,1]);
change(f[i,2],f[j,2]);
change(f[i,3],f[j,3]);
change(f[i,4],f[j,4]);
inc(i); dec(j);
end;
until i>j;
if i<r then kp1(i,r);
if l<j then kp1(l,j);
end;
procedure kp2(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=f[(l+r) shr 1,3];
repeat
while f[i,3]>mid do inc(i);
while f[j,3]<mid do dec(j);
if i<=j then
begin
change(f[i,1],f[j,1]);
change(f[i,2],f[j,2]);
change(f[i,3],f[j,3]);
change(f[i,4],f[j,4]);
inc(i); dec(j);
end;
until i>j;
if i<r then kp2(i,r);
if l<j then kp2(l,j);
end;
begin
assign(input,'mstmn.in'); reset(input);
assign(output,'mstmn.out'); rewrite(output);
readln(n,m);
for i:=1 to m do
begin
readln(ff[i,1],ff[i,2],ff[i,3]);
ff[i,4]:=i;
end;
readln(ff[m+1,1],ff[m+1,2],ff[m+1,3]);
ff[m+1,4]:=m+1;
f:=ff;
kp1(1,m);
for i:=1 to n do fa[i]:=i;
i:=1; j:=1; intree[m+1]:=true;
fa[find(ff[m+1,1])]:=find(ff[m+1,2]);
repeat
inc(i);
if find(f[i,1])<>find(f[i,2]) then
begin
fa[find(f[i,1])]:=find(f[i,2]);
inc(j);
intree[f[i,4]]:=true;
end;
until j=n-1;
f:=ff;
kp1(1,m+1);
for i:=1 to n do fa[i]:=i;
i:=0; j:=0;
repeat
inc(i);
if find(f[i,1])<>find(f[i,2]) then
if intree[f[i,4]] then
begin
fa[find(f[i,1])]:=find(f[i,2]);
inc(j);
end else delete[f[i,4]]:=true;
until j=n-1;
f:=ff;
kp2(1,m);
fillchar(intree,sizeof(intree),#0);
for i:=1 to n do fa[i]:=i;
i:=1; j:=1; intree[m+1]:=true;
fa[find(ff[m+1,1])]:=find(ff[m+1,2]);
repeat
inc(i);
if find(f[i,1])<>find(f[i,2]) then
begin
fa[find(f[i,1])]:=find(f[i,2]);
inc(j);
intree[f[i,4]]:=true;
end;
until j=n-1;
f:=ff;
kp2(1,m+1);
for i:=1 to n do fa[i]:=i;
i:=0; j:=0;
repeat
inc(i);
if find(f[i,1])<>find(f[i,2]) then
if intree[f[i,4]] then
begin
fa[find(f[i,1])]:=find(f[i,2]);
inc(j);
end else delete[f[i,4]]:=true;
until j=n-1;
ans:=0;
for i:=1 to m do
if delete[i] then inc(ans);
writeln(ans);
close(input); close(output);
end.