文章
85
粉丝
0
获赞
531
访问
11.1k
#include <bits/stdc++.h>
using namespace std;
const int Max = 105;
int fa[Max];
struct node {
int u,v,w;
};
int find(int x) {
if (fa[x]== x) {
return x;
}
fa[x] = find(fa[x]);
return fa[x];
}
void myunion(int a ,int b){
fa[find(a)] = find(b);
}
bool cmp(node a,node b) {
return a.w<b.w;
}
int main() {
int N;
while (cin>>N) {
if (N == 0) {
break;
}
int M;
cin>>M;
for (int i=1;i<=M;i++) {
fa[i] = i;
}
int sum = 0;
int count = 0;
vector<node> nodes;
while (N--) {
int u, v, w;
cin>>u>>v>>w;
nodes.push_back({u,v,w});
}
sort(nodes.begin(), nodes.end(), cmp);
for (node n : nodes) {
int cu = n.u;
int cv = n.v;
if (find(cu) != find(cv)) {
...
登录后发布评论
暂无评论,来抢沙发