time limit per test :1 second
memory limit per test : 256 megabytes
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n
vertices and m weighted edges. There are k special vertices: x1,x2,…,xkx_1,x_2,…,x_kx1,x2,…,xk.
Let’s define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
Input
The first line contains three integers nnn, mmm and k(2≤k≤n≤105,n−1≤m≤105)k (2≤k≤n≤10^5, n−1≤m≤10^5 )k(2≤k≤n≤105,n−1≤m≤105) — the number of vertices, the number of edges and the number of special vertices.
The second line contains kkk distinct integers x1,x2,…,xk(1≤xi≤n)x_1,x_2,…,x_k (1≤xi≤n)x1,x2,…,xk(1≤xi≤n).
Each of the following m lines contains three integers u,vu, vu,v and w(1≤u,v≤n,1≤w≤109)w (1≤u,v≤n,1≤w≤10^9)w(1≤u,v≤n,1≤w≤109), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge(u,v)(u,v)(u,v)can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
Output
The first and only line should contain kintegers. The i−thi-thi−th integer is the distance between xix_ixi and the farthest special vertex from it.
Examples
Input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
Output
2 2
Input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
Output
3 3 3
Note
In the first example, the distance between vertex 111 and 222 equals to $24 because one can walk through the edge of weight 222 connecting them. So the distance to the farthest node for both 111 and 222 equals to 222.
In the second example, one can find that distance between 111 and 222, distance between 111 and 333 are both 333 and the distance between 222 and 333 is 222.
The graph may have multiple edges between and self-loops, as in the first example.
题意:
给一个nnn个点mmm条无向边的连通图,这nnn个点中有kkk个是特殊点,定义一条路径的长度是这条路径上最长的边的长度,两个点之间的距离是这两个点之间的所有路径的最短长度,图中可能有重边和自环。
对于每个特殊点xix_ixi来说,设xjx_jxj是和xix_ixi距离最远的特殊点,对于每个xix_ixi求出这个最长距离。
题解:
我们可以发现答案一定是最小生成树上的一条边,而且所有xix_ixi的答案都相同(任何一个在这条边一侧的特殊点都可以选择在这条边的另一侧的特殊点作为离自己距离最远的特殊点),那么只有当选取的边的两侧都有特殊点的时候他才可以被算作答案。我们就可以在计算最小生成树合并点集的时候记录一下一个点集中是否有特殊点来判断这个边是否需要用于更新答案。最后把答案输出kkk次就行了。
#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
struct edge{
int u,v,w;
}e[100004];
int f[100004],n,m,k;
int sp[100004];
inline bool dex(edge A,edge B){return A.w<B.w;}
int Find(int x){return (f[x]==x)?x:f[x]=Find(f[x]);}
int w33ha(){
memset(sp,0,sizeof(sp));
for(int i=1;i<=n;i++)f[i]=i;
int x;
for(int i=1;i<=k;i++){
scanf("%d",&x);
sp[x]=1;
}
for(int i=1;i<=m;i++)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
sort(e+1,e+m+1,dex);
int ans=0;
for(int i=1;i<=m;i++){
int p=Find(e[i].u),q=Find(e[i].v);
if(p!=q){
if(sp[p]&&sp[q])ans=max(ans,e[i].w);
else if(sp[p]||sp[q]){
sp[p]=1;
sp[q]=1;
}
f[p]=q;
}
}
for(int i=1;i<k;i++)printf("%d ",ans);
printf("%d\n",ans);
return 0;
}
int LiangJiaJun(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF)w33ha();
return 0;
}