裸的最小生成树问题,做的时候可以考虑了好久坑点在哪,最后也没发现有啥问题,直接交了kruskal就A了,,
kruskal不会的可以考虑面壁思过或者自行百度,,
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 1000;
const int maxm = 100000 + 200;
struct edge {
int from, to, val;
};
int father[maxn];
int tot = 0;
int n, m;
int x, y, z;
edge p[maxm];
int anss, ansm = 0;
bool cmp(edge aa, edge bb) {
return (aa.val < bb.val);
}
int getfather(int x) {
if (father[x] == x) return (x);
return (father[x] = getfather(father[x]));
}
int main () {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &p[i].from, &p[i].to, &p[i].val);
}
std :: sort(&p[1], &p[m+1], cmp);
for (int i = 1; i <= n; i++) father[i] = i;
for (int i = 1; i <= m; i++) {
int tx = getfather(p[i].from);
int ty = getfather(p[i].to);
if (tx == ty) continue;
father[tx] = ty;
anss++;
ansm = std :: max(ansm, p[i].val);
}
printf("%d %d", anss, ansm);
return 0;
}