AcWing 1142. 繁忙的都市
原题链接
简单
作者:
Value
,
2020-10-13 10:28:41
,
所有人可见
,
阅读 328
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 8010;
struct node{
int x, y, w;
}ege[N];
int n, m;
void read(){
cin >> n >> m;
for(int i = 0; i < m; i ++ ) cin >> ege[i].x >> ege[i].y >> ege[i].w;
}
int res = 0;
int parent[N];
bool cmp(node a, node b){
return a.w < b.w;
}
int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void kruskal(){
for(int i = 0; i <= n; i ++ ) parent[i] = i;
sort(ege, ege + m, cmp);
for(int i = 0; i < m; i ++ ){
int px = find(ege[i].x), py = find(ege[i].y), w = ege[i].w;
if(px != py){
res = w;
parent[px] = py;
}
}
}
int main(){
read();
kruskal();
cout << n - 1 << ' ' << res << endl;
return 0;
}