牛客网暑期ACM多校训练营(第六场)G
https://www.nowcoder.com/acm/contest/144/G
链接:https://www.nowcoder.com/acm/contest/144/G
来源:牛客网
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
-->
输入
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的更多相关文章
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期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 ,问你 ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
- 牛客网暑期ACM多校训练营(第九场)D
链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...
- 牛客网暑期ACM多校训练营(第二场)B discount
链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 牛客网暑期ACM多校训练营(第二场) I Car 思维
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...
- 牛客网暑期ACM多校训练营(第二场) D money 思维
链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...
随机推荐
- lavarel功能总结
详细可参见笔记:laraval学习笔记(二) 路由 route 绑定模型,绑定参数 模版 blade .blade.php后缀,有laravel自己的模版语法 模型 model 如果用create创建 ...
- c# 移除数组为空元素
通过字符串分割取得的数组中,有的时候可能需要处理除掉数组元素值为空的情况,所以可以按照如下代码完成: string rs="";//需要分割的字符串 char[] chspilt= ...
- Oracle ORA
ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常. ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 OR ...
- LC.exe 已退出,代码为-1 问题解决
最近一个c#工程,之前编译正常.后重装系统,安装DevExpress后,编译一直失败,并提示"4>C:\Windows\Microsoft.NET\Framework\v4.0.303 ...
- php-PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'xxx.so' in Unknown on line 0
关于xxx.so,今天在安装php的模块时候老是报,xxx.so的问题,虽然不影响使用,但作为一名当年的程序员强迫症患者,誓死要把 他搞清楚,后面发现是删除了也没有影响,因为在安装php的时候已经将他 ...
- cyclic swapping algorithm
原文见:https://leetcode.com/problems/couples-holding-hands/discuss/113362/JavaC%2B%2B-O(N)-solution-usi ...
- Graveyard LA3708
白书第一章例题4 思维. 先固定一点不动,假设最后一共N个点,那么编号为0,1,...N-1, 0不动,原来的n个点分别占据i/n*N的位置(记为pos),移动到pos四舍五入的位置即可. 证明一:有 ...
- javaweb系列-关于HttpSessionListener的sessionDestroyed什么时候触发
根据书本写了下面这个监听器,然后开始调试,打开一个浏览器来访问该网页,可以正常触发sessionCreated,然后关闭浏览器,发现没有触发sessionDestroyed,然后我怀疑是不是这个监听器 ...
- 【lua实战摸索】在b.lua调用a.lua的函数
需要掌握知识: lua table的使用(创建自己函数的表作为函数库) 普通函数的调用:tab.func(tab,参数) 等效于表中函数的调用tab:func(参数) 基本思路: 1.在相同目录下创建 ...
- fio测试nvme性能
#cat /sys/block/nvme0n1/queue/scheduler none #cat /sys/block/sda/queue/scheduler noop deadline [cfq] ...