牛客网暑期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 ...
随机推荐
- codevs 1043 方格取数 2000年NOIP全国联赛提高组
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 设有N*N的方格图(N<=10,我们将其中的某些方格中填入正整数,而 ...
- C++#pragma pack指令
微软官方文档说#pragma pack 指令的作用是为结构.联合和类成员指定 pack 对齐.的主要作用就是改变编译器的内存对齐方式,这个指令在网络报文的处理中有着重要的作用,#pragma pack ...
- System.currentTimeMillis()与日期之间的相互转换
System.currentTimeMillis()与日期 之间是可以相互转换的,大多数Android开发者都知道 通过 SimpleDateFormat dateformat = new Simpl ...
- oracle插入中文乱码
执行sql: select userenv('language') from dual; -- AMERICAN_AMERICA.ZHS16GBK select * from v$nls ...
- 指针-动态开点&合并线段树
一个知识点不在一道题里说是没有灵魂的 线段树是用来处理区间信息的咯 但是往往因为需要4倍空间让许多人退却,而动态开点的线段树就非常棒 仿佛只用2倍就可以咯 指针保存位置,即节点信息,是很舒适的,所以用 ...
- 自己封装一个readline函数实现服务器客户端回射
实现的功能:一次只能读取一行,客户端输入之后,一回车,马上字符串传到服务器端并显示在终端,然后服务器端将字符串又传回给客户端. 服务器端可以接收多个客户端的连接请求,并fork一个子进程来进行服务. ...
- 目标检测中bounding box regression
https://zhuanlan.zhihu.com/p/26938549 RCNN实际包含两个子步骤,一是对上一步的输出向量进行分类(需要根据特征训练分类器):二是通过边界回归(bounding-b ...
- 695. Max Area of Island@python
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- LeetCode(42)Trapping Rain Water
题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- JavaIO基础学习笔记
JavaIO JavaIO即Java的输入输出系统.比如我们的程序要读取一个文本文件.一张图片或者要获取控制台输入的内容,就要用到输入流:又或者程序要将生成的一段字符窜以文件的形式保存到系统中就要用到 ...