[CF915F]Imbalance Value of a Tree
[CF915F]Imbalance Value of a Tree
题目大意:
一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\)。定义\(I(i,j)\)为\(i\)到\(j\)之间简单路径上最大权值与最小权值之差,求\(\displaystyle\sum_{i=1}^n\sum_{j=1}^nI(i,j)\)。
思路:
分别计算路径最大权值之和与最小权值之和。以最大权值之和为例,在图中按权值从大到小枚举每一个点,则对于该连通块中每一个经过该点的路径,该点为路径上权值最大的点,可以计算该点对答案的贡献,并将计算完贡献的点从图中删去。
由于删点是一个难以实现的操作,因此可以将操作改为加点操作,用并查集维护连通性即可。最小权值和同理。时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<numeric>
#include<algorithm>
#include<forward_list>
using int64=long long;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
constexpr int N=1e6+1;
int seq[N],pos[N],w[N];
std::forward_list<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].emplace_front(v);
e[v].emplace_front(u);
}
struct DisjointSet {
int anc[N],size[N];
void reset(const int &n) {
std::fill(&size[1],&size[n+1],1);
std::iota(&anc[1],&anc[n+1],1);
}
int find(const int &x) {
return x==anc[x]?x:anc[x]=find(anc[x]);
}
void merge(const int &x,const int &y) {
size[find(y)]+=size[find(x)];
anc[find(x)]=find(y);
}
};
DisjointSet s;
int main() {
const int n=getint();
for(register int i=1;i<=n;i++) w[i]=getint();
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
int64 max=0,min=0;
s.reset(n);
std::iota(&seq[1],&seq[n+1],1);
std::sort(&seq[1],&seq[n+1],[](const int &a,const int &b){return w[a]<w[b];});
for(register int i=1;i<=n;i++) pos[seq[i]]=i;
for(register int i=1;i<=n;i++) {
const int &x=seq[i];
int64 last=1,tmp=0;
for(register auto &y:e[x]) {
if(pos[y]>pos[x]) continue;
tmp+=last*s.size[s.find(y)];
last+=s.size[s.find(y)];
s.merge(x,y);
}
max+=(int64)w[x]*tmp;
}
s.reset(n);
std::iota(&seq[1],&seq[n+1],1);
std::sort(&seq[1],&seq[n+1],[](const int &a,const int &b){return w[a]>w[b];});
for(register int i=1;i<=n;i++) pos[seq[i]]=i;
for(register int i=1;i<=n;i++) {
const int &x=seq[i];
int64 last=1,tmp=0;
for(register auto &y:e[x]) {
if(pos[y]>pos[x]) continue;
tmp+=last*s.size[s.find(y)];
last+=s.size[s.find(y)];
s.merge(x,y);
}
min+=(int64)w[x]*tmp;
}
printf("%lld\n",max-min);
return 0;
}
[CF915F]Imbalance Value of a Tree的更多相关文章
- CF915F Imbalance Value of a Tree (并查集)
题目大意:给你一棵树,每个点有点权a_{i},求$\sum _{i=1}^{n} \sum _{j=i}^{n} f(i,j)$,$f(i,j)$表示i,j,路径上的点的最大权值-最小权值 正解的思路 ...
- Codeforces 915F Imbalance Value of a Tree
Imbalance Value of a Tree 感觉这种题没啥营养, 排个序算算贡献就好啦. #include<bits/stdc++.h> #define LL long long ...
- 【CodeForces】915 F. Imbalance Value of a Tree 并查集
[题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...
- Codeforces 915F Imbalance Value of a Tree(并查集)
题目链接 Imbalance Value of a Tree 题意 给定一棵树.求树上所有简单路径中的最大权值与最小权值的差值的和. 首先考虑求所有简单路径中的最大权值和. 对所有点按照权值大小升 ...
- Codeforces 915 F. Imbalance Value of a Tree(并查集)
F. Imbalance Value of a Tree 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
- Imbalance Value of a Tree CodeForces - 915F
链接 大意: 给定树, 求树上所有链上最大值最小值之差 817D的树上版本, 用并查集维护即可. 817D由于是链的情况并查集不必压缩路径即可达到均摊$O(n)$, 该题必须压缩, 复杂度$O(nlo ...
- Codeforces915F. Imbalance Value of a Tree
n<=1e6的树问所有路径的极差之和. 被遗忘的套路...以后绝对不会再忘了QAQ 只要算最大值之和即可,最小值同理.数字从大到小排序(反正都是要排序的,如果从大到小不行等会反过来试试),然后逐 ...
- 记第一场cf比赛(Codeforces915)
比赛感想 本来21:05开始的比赛,结果记成21:30了...晚了25分钟才开始[捂脸] 这次是Educational Round,所以还比较简单. 前两道题一眼看去模拟+贪心,怕错仔细看了好几遍题, ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
随机推荐
- Firefox多国语言多OS离线安装包
Download Firefox in your language Firefox is made in large part by volunteers around the world. That ...
- Mybatis如何查询部分字段
解决问题:数据库表里面很多字段不太需要,有时只想取到里面的部分字段的值,如果重新定义 DTO 会比较麻烦. BookMapper.xml 文件中定义如下: ` <!-- Book全部字段 --& ...
- centos 下构建lamp环境
构建准备: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp - ...
- POJ 3617 Best Cow Line (模拟)
题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...
- JHDU 2601 An easy problem (数学 )
title: An easy problem 数学 杭电2601 tags: [数学] 题目链接 Problem Description When Teddy was a child , he was ...
- HDU 1840 Equations (数学)
title: Equations 数学 杭电1840 tags: [数学] 题目链接 Problem Description All the problems in this contest tota ...
- bzoj 1901 线段树套平衡树+二分答案查询
我们就建一颗线段树,线段树的每一个节点都是一颗平衡树,对于每个询问来说,我们就二分答案, 查询每个二分到的mid在这个区间里的rank,然后就行了 /************************* ...
- js判断浏览器是否为ie
使用传统方式 if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Op ...
- 获取高德地图api
先到高德开放平台首页按照关键字搜索地址,获取经纬度坐标: http://lbs.amap.com/console/show/picker 高德由坐标获取地址详细信息: http://restapi.a ...
- tmux下make menuconfig背景色不正常问题
参考https://blog.tankywoo.com/2015/10/24/tmux-mutt-not-redraw-problem.html 是由于~/.bashrc或~/.zshrc设置,覆盖了 ...