[树剖?]bzoj2435: [Noi2011]道路修建

bzoj2435: [Noi2011]道路修建:http://www.lydsy.com/JudgeOnline/problem.php?id=2435

用树剖的东西搞一搞节点数tot
其实就是一个暴力。。。
noi怎么会有这么简单的题

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct node
{
    int x,y,next; 
    long long c;
}a[2100000];
int last[1100000],len;
int ex[1100000],ey[1100000];
long long ec[1100000];
int tot[1100000];//以该点为根的子树节点
int fa[1100000];
void build(int x,int y,long long c)
{
    len++;
    a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
}
void dfs(int x)
{
    tot[x]=1;
    for (int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if (fa[x]!=y)
        {
            fa[y]=x;
            if (tot[y]==0) dfs(y);
            tot[x]+=tot[y];
        }
    }
}
int main()
{
    int n;
    long long ans=0;
    scanf("%d",&n);
    for (int i=1;i<n;i++)
    {
        scanf("%d%d%lld",&ex[i],&ey[i],&ec[i]);
        build(ex[i],ey[i],ec[i]);
        build(ey[i],ex[i],ec[i]);
    }
    for (int i=1;i<=n;i++) fa[i]=i;
    memset(tot,0,sizeof(tot));
    dfs(1);
    for (int i=1;i<n;i++)
    {
        int s1,s2;
        s1=min(tot[ex[i]],tot[ey[i]]);
        s2=n-s1; //有可能大的tot是包含了边的另一端节点的 选小的万无一失(不要问我为什么知道的ovo) 
        ans+=(long long)ec[i]*abs(s1-s2);
    }
    printf("%lld\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值