[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)便是使用二叉树实现 ...
随机推荐
- intellij IDEA与springboot项目建立
概念问题: IntelliJ系中的Project相当于Eclipse系中的workspace.IntelliJ系中的Module相当于Eclipse系中的Project.IntelliJ中一个Proj ...
- elk,centos7,filebeat,elasticsearch-head详细安装步骤
先来张图,大致结构应该晓得,对吧! 安装jdk:至少1.8以上 yum -y localinstall jdk-8u73-linux-x64.rpm 安装elasticsearch5.2.1 用普通用 ...
- apply()和call()
每个函数都包含俩个非继承而来的方法:apply() 和 call(),这俩个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值,以扩充函数赖以运行的作用域.一般来讲,thi ...
- oracle中函数
一:前言 最近决定每天都把知识点总结下,然后每个星期把知识点在进行分类发表日志. 二:The Question (1):在oracle中进行年龄的计算,知道出生日期进行计算后截取,本来是一个很简单的函 ...
- uva 11427
题目大意:每天晚上你都玩纸牌,如果第一次赢了就高高兴兴地去睡觉:如果输了就接着玩,假设每盘游戏获胜的的概率都是p,且各盘游戏相互独立.你是一个固执的完美主义者,因此会一直玩到当晚获胜局数的比例严格大于 ...
- 【数据结构】bzoj2957楼房重建
Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...
- Django项目知识点汇总
目录 一.wsgi接口 二.中间件 三.URL路由系统 四.Template模板 五.Views视图 六.Model&ORM 七.Admin相关 八.Http协议 九.COOKIE 与 SES ...
- bzoj 1012 基础线段树
原题传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 今儿一天状态不好,都没怎么刷题..快下课了,刷道水题.... 裸的线段树 /*** ...
- inno setup 5 添加快捷方式默认选中
转载:https://www.cnblogs.com/x_wukong/p/5012412.html https://zhidao.baidu.com/question/312006120.html ...
- python基础复习-1-2 数据类型-str、list、tuple、dict
数据类型 数字 引号: 123 数值 '123' 字符串 整数:ini long 范围:(-2**31 - 2**31) num = 123 长整型 long (L) num = 123L 浮点型:f ...