题目:http://acm.hdu.edu.cn/showproblem.php?pid=4514
/*
这道题是一个带权并查集的题目。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn = 100005;
int father[maxn];
int rank[maxn];
int weight[maxn];
void make_set(int n){
for(int i = 1;i <=n;i++){
father[i] =i;
rank[i] =0;
weight[i] =0;
}
}
int find_set(int x){
if(father[x] != x)
father[x] =find_set(father[x]);
return father[x];
}
int unit_set(int x,int y,int w){
x = find_set(x);
y = find_set(y);
if(x == y)
return 0;
if(rank[x] > rank[y]){
father[y] =x;
weight[x] +=(weight[y] + w);
}
else{
if(rank[x] == rank[y])
rank[y]++;
father[x] =y;
weight[y] +=(weight[x] + w);
}
return 1;
}
int main(){
int n,m;
int u,v,w;
while(~scanf("%d%d",&n,&m)){
int flag =0;
make_set(n);
for(int i = 0;i <m;i++){
scanf("%d%d%d",&u,&v,&w);
if(flag)
continue;
if(unit_set(u, v, w))
continue;
else
flag = 1;
}
int ans =0;
if(flag)
printf("YES\n");
else{
for(int i = 1;i <= n;i++)
if(ans<weight[i])
ans = weight[i];
printf("%d\n",ans);
}
}
return 0;
}