YBTOJ 路径求和

题面:

image

image

题目分析:

考虑每一条边对于答案的贡献,根据树的性质,每一条边一定将整棵树分成了两个子树,而该边的贡献=子树1中叶节点数子树2中节点总数+子树2中叶节点数子树1中节点总数。

由于树有一个重要性质:

树的任意一个点都可以作为树的根

然后?没了。

哦不是,还有一个重要的点:

要注意乘法算式过程中的点要尽量多开long long,因为会爆int
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+100;
int head[maxn],ecnt=-1,m,a,b,c,du[maxn],root;
long long ans;
long long leaf[maxn],tot[maxn],n,totleaf; 
struct mint
{
	int nxt,v,w;	
}e[maxn<<1];
inline void addline(int u,int v,int w)
{
	e[++ecnt].nxt=head[u];
	e[ecnt].v=v;
	e[ecnt].w=w;
	head[u]=ecnt;	
}
void dfs(int now,int fa)
{
	tot[now]=1;
	if(du[now]==1) 
	{
		totleaf++;
		leaf[now]=1;	
	}
	for(int i=head[now];~i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==fa) continue;
		dfs(v,now);
		leaf[now]+=leaf[v];
		tot[now]+=tot[v];
	}
	return;
}
void find(int now,int fa)
{
	for(int i=head[now];~i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==fa) continue;
		find(v,now);
		ans+=(long long)e[i].w*(leaf[v]*(n-tot[v])+tot[v]*(totleaf-leaf[v]));
	}
}
int main()
{
	memset(head,-1,sizeof(head));
	scanf("%lld%d",&n,&m);
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d%d",&a,&b,&c);
		du[c]++; 
		du[b]++;
		addline(b,c,a);
		addline(c,b,a);
	}
	dfs(1,0); 
	find(1,0);
	printf("%lld",ans);
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值