CSP2018_12_4
package practiceofcsp;
import java.util.Arrays;
import java.util.Scanner;
public class CSP2018_12_4_me {
static int pre[];//存贮根节点
static int find(int x) {//寻找根节点
if(pre[x]==x) {
return x;
}
else {
return pre[x]=find(pre[x]);
}
}
static boolean iscircle(int x,int y) {//x和y连接是否能构成环
if(find(x)==find(y)) {
return false;
}
else {
return true;
}
}
static void islink(int x,int y) {//注意是根连接
int xx=find(x);
int yy=find(y);
pre[yy]=xx;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int root = input.nextInt();
pre = new int[n+1];
for(int i=0;i<pre.length;i++) {
pre[i]=i;
}
Edge edge[]=new Edge[m];
for(int i=0;i<edge.length;i++) {
edge[i]=new Edge();
edge[i].from=input.nextInt();
edge[i].to=input.nextInt();
edge[i].time=input.nextInt();
}
int count =0;
Arrays.sort(edge);
int max=0;
for(int i=0;i<edge.length&&count<n-1;i++) {
Edge edgei=new Edge();
edgei=edge[i];
if(iscircle(edgei.from,edgei.to)){
islink(edgei.from,edgei.to);
count++;
max=edgei.time;
}
}
System.out.println(max);
}
}
class Edge implements Comparable<Edge>{
int from;
int to;
int time;
@Override
public int compareTo(Edge o) {
// TODO Auto-generated method stub
return time-o.time;
}
}