题意:
求最小生成树的最大边。
解析:
kruscal。
昨天刚做的一道一模一样的题。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
using namespace std;
const int maxn = 20000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int n,m;
int fa[maxn];
int ans;
struct edge
{
int fr,to,value;
bool operator<(const edge& a)const
{
return value<a.value;
}
} e[maxn];
void Init()
{
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
}
int Find(int x)
{
if (fa[x] != x)
fa[x] = Find(fa[x]);
return fa[x];
}
int Kruscal()
{
int res = 0;
sort(e, e + m);
for (int i = 0; i < m; i++)
{
int u = Find(e[i].fr);
int v = Find(e[i].to);
if (u != v)
{
fa[v] = u;
res += e[i].value;
ans = max(ans, e[i].value);
}
}
return res;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
while (~scanf("%d%d", &n, &m))
{
ans = 0;
Init();
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &e[i].fr, &e[i].to, &e[i].value);
}
Kruscal();
printf("%d\n", ans);
}
return 0;
}