牛客网暑期ACM多校训练营(第六场)G

https://www.nowcoder.com/acm/contest/144/G

链接:https://www.nowcoder.com/acm/contest/144/G
来源:牛客网

In Viridian forest there is a tree T formed by N nodes, each edge on which has a positive weight.

There is an undirected graph G generated from tree T, which contains N nodes and undirected edges, where the capacity of the edge between u and v equals to the distance between them on the tree T.

Given the tree T, Pikachu wants to calculate the sum of the max flow between every two nodes in G, there are different pairs of nodes should be counted. Could you help him?

输入描述:

The input starts with one line containing exactly one integer t, which is the number of test cases.

For each test case, the first line contains one integer N, indicating the size of the tree T.

Then followed by N - 1 lines, each consists of three integers u

i

, v

i

 and w

i

, representing the two nodes connected by the i-th edge and the weight of the i-th edge.

- 1 ≤ t ≤ 10.
- 2 ≤ N ≤ 10

5

.

- 1 ≤ wi≤ 1000.
- 

输出描述:

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the sum of the maximum flow between every two nodes in G.

示例1

输入

2
3
1 2 1
2 3 1
5
1 2 1
2 3 1
2 4 1
4 5 2

输出

Case #1: 7
Case #2: 72


题解

 

 

我感觉他说的显然那句话  显然是错的啊  但是答案貌似是他说的那样啊 找不到反例啊   不知道对不对啊  没人解答啊 

就当做 求树上所有点的距离之和  来做吧

代码

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl;
using namespace std;
typedef long long ll;
const ll maxn=1e5+50,inf=1e18;
const ll mod=1e15;
ll f[maxn],h[maxn],sz[maxn];
vector<pair<ll,ll>> g[maxn];
int n;
void dfs1(int x,int y)
{
    sz[x]=1;
    f[x]=0;
    for(int i=0;i<g[x].size();i++)
    {
        int u=g[x][i].fi;
        ll w=g[x][i].se;
        if(u==y)continue;
        dfs1(u,x);
        sz[x]+=sz[u];
        f[x]+=f[u]+sz[u]*w;
    }
}
void dfs2(int x,int y)
{
    for(int i=0;i<g[x].size();i++)
    {
        int u=g[x][i].fi;
        ll w=g[x][i].se;
        if(u==y)continue;
        h[u]=h[x]+f[x]-f[u]-sz[u]*w+(n-sz[u])*w;
        dfs2(u,x);
    }
    f[x]+=h[x];
}
int main()
{
    int t,kase=1;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            h[i]=f[i]=sz[i]=0;
            g[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            g[u].pb(mp(v,w));
            g[v].pb(mp(u,w));
        }
        dfs1(1,0);
        dfs2(1,0);
        ll ans1=0,ans2=0;
        sort(f+1,f+1+n);
        for(int i=1;i<=n;i++)
        {
            ll temp=f[i]*(n-i); //第一小的贡献了n-1次  以此类推
            ans2+=temp%mod;
            ans1+=temp/mod;
            if(ans2>=mod)      //相加过程会爆ll 所以这样处理一下 分成前后15位
            {
                ans2-=mod;
                ans1++;
            }
        }
        printf("Case #%d: ",kase++);
        if(ans1)
            printf("%lld%015lld\n",ans1,ans2);
        else
            printf("%lld\n",ans2);
    }
}

 

 
    

转载于:https://www.cnblogs.com/stranger-/p/9445440.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值