题目:
分析:
类似
k
r
u
s
k
a
l
kruskal
kruskal算法的过程,对于一条边,如果它们的两个点属于两个不同的集合,那么这些集合间都要连边,为保证最小生成树还是数据给出的树,我们就让它们都连上
v
+
1
v+1
v+1,因为我们需要保证原来树仍是最小生成树
(详见yd书
P
339
P339
P339)
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<ctime>
#include<iomanip>
#include<string>
#include<bitset>
#include<deque>
#include<set>
#define LL long long
#define ch cheap
using namespace std;
inline LL read() {
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
struct node{
LL x,y,w;
}e[12005];
LL f[6005],s[6005];
LL find(LL i)
{
return f[i]==i?i:f[i]=find(f[i]);
}
bool cmp(node x,node y) {return x.w<y.w;}
int main()
{
LL n,t=read(),ans;
while(t--)
{
ans=0;
n=read();
for(LL i=1;i<n;i++)
{
e[i].x=read();e[i].y=read();e[i].w=read();
}
sort(e+1,e+n,cmp);
for(LL i=1;i<=n;i++) f[i]=i,s[i]=1;
for(LL i=1;i<n;i++)
{
LL a,b;
a=find(e[i].x);b=find(e[i].y);
if(a==b) continue;
f[a]=b;
ans+=(e[i].w+1)*(s[a]*s[b]-1);
s[b]+=s[a];
}
printf("%lld\n",ans);
}
return 0;
}