牛客网暑期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 ...
随机推荐
- ubuntu下php安装目录说明
php当前安装目录 /etc/php5/ apache2: 采用APACHE2HANDLER启动 cli: 采用命令启动 fpm php-fpm启动 fpm2 php-fpm多实例 m ...
- 项目中常用git命令操作指令(一般正常的话够用不够再看相关git命令)
配置git1.首先在本地创建ssh key:ssh-keygen -t rsa -C "github上注册的邮箱" //(一路回车)2.进入c:/Users/xxxx_000/.s ...
- ftp 报错 200 Type set to A
最近在使用ssis 从ftp服务器抓起文件到本地的时候,发现连接ftp出错 200 Type set to A 解决办法: ftp connection 中 设置UsePassiveMode 为Tru ...
- SEO 第六章
SEO第六章 本次课程目标: 1. 掌握网站的内链优化 2. 网站的URL优化 一. 网站地图 1. 什么是网站地图? 网站地图也叫站点地图,英文名叫sitemap,指的网站所有内 ...
- leetcode_935. Knight Dialer_动态规划_矩阵快速幂
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
- 光线步进——RayMarching入门
入门实现 先用RayMarching描绘一个球体,最后在进行光照计算参考:https://www.shadertoy.com/view/llt3R4 模拟摄像机射线float3 rayDirectio ...
- BZOJ2120 数颜色(树套树)
B. 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令:1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色 ...
- No-9.vi __终端中的编辑器
vi —— 终端中的编辑器 01. vi 简介 1.1 学习 vi 的目的 在工作中,要对 服务器 上的文件进行 简单 的修改,可以使用 ssh 远程登录到服务器上,并且使用 vi 进行快速的编辑即可 ...
- JS第三方中间件的延伸
js中间件 当我们在编写业务代码时候,我们无法避免有些业务逻辑复杂而导致业务代码写得又长又乱,如果再加上时间紧凑情况下写出来的代码估计会更让人抓狂.以至于我们一直在寻求更好的架构设计和更好的代码设计, ...
- CSS3---圆角设置
1.border-radius是向元素添加圆角边框.border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 4px 3px ...