牛客网暑期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 ...
随机推荐
- mac上的应用提权
一个mac上的app需要在/Applications/My.app/Contents/MacOS路径下创建一个配置文件,在开启root权限的账户下运行时ok,但是在没有开启root权限的账户下运行时, ...
- HDU 5380 Travel with candy (贪心,单调队列)
题意: 有n+1个城市按顺序分布在同一直线上,现在需从0号城市按顺序走到n号城市(保证可达),从0号城市到i号城市需要消耗ai个糖果,每个城市都可以通过买/卖糖果来赚取更多的钱,价格分别是buyi和s ...
- 合并百度影音的离线数据 with python 2.3 格式更新
很久没有更新了. 这次新增支持四种格式的解析. filelist slicelist download.cfg third_party_download.cfg 还是2个文件.替换之前版本即可. 初步 ...
- SQLite_安装
SQLite -安装 zero-configuration SQLite闻名的特性,这意味着不需要复杂的设置或管理.本章将带你通过设置SQLite的过程在Windows.Linux和Mac OS X. ...
- SQLite – ORDER 子句
SQLite - ORDER BY子句 The SQLite ORDER BY子句用于数据按升序或降序排序,基于一个或多个列. 语法: ORDER BY子句的基本语法如下: SELECT column ...
- dom和bom是什么?
DOM:文档对象模型,描述了处理网页内容的方法和接口.最根本对象是document(window.document). 由于DOM的操作对象是文档,所以DOM和浏览器没有直接关系. 部署在服务器上的文 ...
- MFC隐藏在黑暗之中的大坑
大坑一:CDC会随着窗口状态的改变而改变 void K5::OnPaint() { CDC *pDC=this->GetDC(); //CDC最好设为局部变量 ... this->Rele ...
- python关于入参中,传入的是指针还是引用
偶然看到别人的代码,发现有的会传入参数之后,做一次copy,试验一下,关于入参中,传入的是指针还是引用先说自己的结论:1.如果传入的是简单的类型,那么传入应该是引用的数值,2.假如传入的是df这种类型 ...
- js模块化入门与commonjs解析与应用
JS模块化的基本原理 commonjs规范 commonjs在前端模块化中的基本使用 AMD与CMD规范剖析博客链接 一.JS模块化基本原理 在JS没有提出来模块化的时候,开发JS项目比较简单,同时也 ...
- 使用 隧道技术 使用http代理连接 svn:// git://
问题点 在某些情况下 无法通过代理 用 svn 访问svn://协议例如(svn://www.qdac.cc ) 故此有了此贴 远端需要一个代理 服务器 connect-tunnel -P 代理 ...