zoukankan      html  css  js  c++  java
  • 牛客网暑期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("
    ");
    #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
    ",ans1,ans2);
            else
                printf("%lld
    ",ans2);
        }
    }
    
    
  • 相关阅读:
    CentOS7设置tomcat开机自启动
    MySQL复制表结构和表数据
    SpEL表达式总结
    Flink快速入门
    Flink架构、原理与部署测试
    第十一篇:Spark SQL 源码分析之 External DataSource外部数据源
    第十篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 query
    第九篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 cache table
    第八篇:Spark SQL Catalyst源码分析之UDF
    第七篇:Spark SQL 源码分析之Physical Plan 到 RDD的具体实现
  • 原文地址:https://www.cnblogs.com/stranger-/p/9445440.html
Copyright © 2011-2022 走看看