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.

输入例子:
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

-->

示例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+,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]=;
f[x]=;
for(int i=;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=;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=;
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<=n;i++)
{
h[i]=f[i]=sz[i]=;
g[i].clear();
}
for(int i=;i<n;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u].pb(mp(v,w));
g[v].pb(mp(u,w));
}
dfs1(,);
dfs2(,);
ll ans1=,ans2=;
sort(f+,f++n);
for(int i=;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);
}
}

牛客网暑期ACM多校训练营(第六场)G的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  2. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  3. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  4. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  5. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  6. 牛客网暑期ACM多校训练营(第九场)D

    链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...

  7. 牛客网暑期ACM多校训练营(第二场)B discount

    链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...

  8. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  9. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

  10. 牛客网暑期ACM多校训练营(第二场) D money 思维

    链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...

随机推荐

  1. IOS访问webserver接口

    接口调用参数只能是字符串格式,返回格式支持3种(字符串,数组,DataSet) 需要引用第三方库,包含(DataSet,PlatServinceDataParser,WebserviceCommon, ...

  2. java将一个List赋值给另一个List的4种方法

    编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...

  3. 基于SAE的Python+Django部署

    本文主要参考:http://www.cnblogs.com/qtsharp/archive/2012/01/12/2320774.html,另外包括自己的实际操作. 一.申请SAE帐号以及创建应用ya ...

  4. SQLite -分离数据库

    SQLite -分离数据库 SQLite分离DTABASE语句用于分离和分离命名数据库从一个数据库连接之前附加使用附加语句.如果相同的数据库文件已附加多个别名,然后分离命令将断开只有名字和其他依附仍将 ...

  5. elasticsearch时间格式DateFormat的含义

    时间格式 枚举(或者英文)format pattern 含义 custom - 自定义属性 none - 不转化 basic_date yyyyMMdd 基本时间 basic_date_time   ...

  6. linux AC的应用详解

    NAME ac - 输出用户连接时间 总览 ac [ -d | --daily-totals ] [ -y | --print-year ] [ -p | --individual-totals ] ...

  7. Spring_对缓存的支持

    使用SpringBoot开启缓存分为两步: 开启基于注解的缓存 标注缓存注解即可 如上就是一个简单的缓存示例 默认使用的是ConcurrentHashMap组件用来缓存的 package ustc.a ...

  8. 【传智播客】Libevent学习笔记(五):基本类型和函数

    目录 00. 目录 01. 基本类型 1.1 evutil_socket_t类型 1.2 标准类型 1.3 各种兼容性类型 02. 可移植的定时器函数 03. 套接字API兼容性 04. 可移植的字符 ...

  9. LayuI固定块关闭

    1.近期项目使用了layui的固定块,但是当到某个独立页面时,固定块还在,就显得突兀: 2.通过F12查看,发现代码: <ul class="layui-fixbar" st ...

  10. js 上传头像img

    <label> <div class="myusercenter-image-none"> <img src="" class=& ...