D. Maximum Distance
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 nn vertices and mm weighted edges. There are kk special vertices: x1,x2,…,xkx1,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 nn, mm and kk (2≤k≤n≤1052≤k≤n≤105, n−1≤m≤105n−1≤m≤105) — the number of vertices, the number of edges and the number of special vertices.
The second line contains kk distinct integers x1,x2,…,xkx1,x2,…,xk (1≤xi≤n1≤xi≤n).
Each of the following mm lines contains three integers uu, vv and ww (1≤u,v≤n,1≤w≤1091≤u,v≤n,1≤w≤109), denoting there is an edge between uu and vv of weight ww. The given graph is undirected, so an edge (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.
#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;
}