B: 论战大原题
时间限制: 1 Sec 内存限制: 128 MB题目描述
Abwad最终造出了一道惊世骇俗的难题——在线询问动态仙人球上第k长的路径的所有后缀的不同的回文子串数,可是nbc只瞄了一眼题面,就说出了Abwad冥思苦想了三天三夜才得到的算法。
为了扭转劣势,Abwad决定和nbc论战大原题。规则很简单,即给出一道原题,比谁能更快地找出原题的出处并将其AC。现在摆在他们面前的是这样一道原题:
给定一个n个点m条边的无向图。定义一条路径的长度为路径上最小边的权值。定义dist(i,j)为起点为i,终点为j的长度最长的路径的长度。求出第k大的dist(i,j)(i<j)。
Abwad依稀记得这道题曾经出现在一场名叫“恩偶爱皮”的比赛中。在搜索引擎的帮助下,他开始以50Hz的手速写起了代码。作为旁观者的你,一眼就看出Abwad看错题了。为了证明他是错的,请你写个程序,求出答案。
输入
第一行两个整数n,m,k。
接下来m行每行三个整数u,v,w,表示u到v存在一条长度为w的无向边。
输出
一行一个整数ans,为第k大的dist(i,j)
样例输入
4 5 21 2 44 3 52 3 24 1 13 1 3
样例输出
4
提示
【样例说明】
dist(1,2)=4 dist(1,3)=3 dist(1,4)=3 dist(2,3)=3 dist(2,4)=3 dist(3,4)=5
故第2大的dist(i,j)为4
【限制与约定】
测试点编号
|
n
|
m
|
k
|
特殊约定
|
1
|
n
≤
100
|
|||
2
|
||||
3
|
||||
4
|
||||
5
|
n
≤
1000
|
m=n-1
|
u=v-1
|
|
6
|
||||
7
|
数据随机生成
|
|||
8
|
||||
9
|
||||
10
|
||||
11
|
n
≤
100000
n
≤
100000
|
k=1
k=1
|
||
12
|
||||
13
|
m=n-1
|
u=v-1
|
||
14
|
||||
15
|
||||
16
|
||||
17
|
数据随机生成
|
|||
18
|
||||
19
|
||||
20
|
对于所有的数据,保证n
≤
100000,m
≤
min(n
2
,200000),k
≤
n(n-1)/2且图连通,w
≤
10
9
。
•n≤100
•Floyed跑出两两之间的dist,统计答案
•时间复杂度O(n^3)
•n≤1000
•首先容易发现,这道大原题的名字叫“货车运输”
•显然只有最大生成树上的边长会作为dist(i,j),在树上dfs即可。
•时间复杂度O(n^2)
•考虑Kruscal过程,当前加的一定是最短的边
•这条边连接的两棵子树,一棵子树到另一棵子树的所有路径必然经过这条边。
•假设它们的大小为size(x),size(y),则dist(i,j)=该边长度的点对就有size(x)*size(y)个
•剩下的问题就很明了了。
•时间复杂度O(mlogn)
var
num,k,root1,root2,n,m:int64;
i,j:longint;
f,u,v,w,sum:array[0..200011] of int64;
procedure swap(var x,y:int64);
var t:int64;
begin
t:=x;x:=y;y:=t;
end;
procedure sort(l,r:int64);
var i,j,m:int64;
begin
i:=l;j:=r;
m:=w[(i+j) div 2];
repeat
while w[i]>m do inc(i);
while w[j]<m do dec(j);
if i<=j then
begin
swap(w[i],w[j]);
swap(u[i],u[j]);
swap(v[i],v[j]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end;
function find(x:int64):int64;
begin
if f[x]<>x then f[x]:=find(f[x]);
exit(f[x]);
end;
begin
readln(n,m,k);
for i:=1 to n do
begin
f[i]:=i;
sum[i]:=1;
end;
for i:=1 to m do readln(u[i],v[i],w[i]);
sort(1,m);
num:=0;
for i:=1 to m do
begin
root1:=find(u[i]);
root2:=find(v[i]);
if root1<>root2 then
begin
num:=num+sum[root1]*sum[root2];
if num>=k then
begin
writeln(w[i]);
close(input);
close(output);
halt;
end;
f[root1]:=root2;
sum[root2]:=sum[root2]+sum[root1];
end;
end;
end.