Imbalance Value of a Tree CodeForces - 915F
大意: 给定树, 求树上所有链上最大值最小值之差
817D的树上版本, 用并查集维护即可.
817D由于是链的情况并查集不必压缩路径即可达到均摊$O(n)$, 该题必须压缩, 复杂度$O(nlogn)$
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const int N = 1e6+10, INF = 0x3f3f3f3f;
int n;
pii a[N];
vector<int> g[N];
ll ans;
int fa[N], sz[N];
int Find(int x) {return fa[x]==x?x:fa[x]=Find(fa[x]);} void solve() {
sort(a+1,a+1+n);
REP(i,1,n) fa[i]=0,sz[i]=1;
REP(i,1,n) {
int x = a[i].y, w = a[i].x;
fa[x] = x;
for (int y:g[x]) if (fa[y]) {
y = Find(y);
ans += (ll)sz[x]*sz[y]*w;
sz[x] += sz[y];
fa[y] = x;
}
}
} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d", &a[i].x), a[i].y=i;
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
solve();
REP(i,1,n) a[i].x=-a[i].x;
solve();
printf("%lld\n", ans);
}
Imbalance Value of a Tree CodeForces - 915F的更多相关文章
- Codeforces 915F Imbalance Value of a Tree
Imbalance Value of a Tree 感觉这种题没啥营养, 排个序算算贡献就好啦. #include<bits/stdc++.h> #define LL long long ...
- 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 [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...
- Codeforces 915 F. Imbalance Value of a Tree(并查集)
F. Imbalance Value of a Tree 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- [CF915F]Imbalance Value of a Tree
[CF915F]Imbalance Value of a Tree 题目大意: 一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\).定义\(I(i,j)\)为\(i\)到\ ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
随机推荐
- python管道pipe,两个进程,使用管道的两端分别执行写文件动作,带锁(lock)
#coding=utf-8import multiprocessing as mp def write_file(content,lock): lock.acquire() with op ...
- 设置(更改)Mysql 自增ID的起始值
SELECT * FROM segwords WHERE id>790511 DELETE FROM segwords WHERE id>790511 #下面这句是设置的 ALTER TA ...
- 关于QStandardItemModel
类QabstractItemModel,QabstractListModel,QAbstractTableModel不保存数据,用户需要从这些类派生出子类,并在子类中定义某种数据结构来保存数据.与此不 ...
- Rapid 2D-to-3D conversion——快速2D到3D转换
https://blog.csdn.net/qq_33445835/article/details/80143598 目前想做一个关于2D转3D的项目,由于国内资料比较少而且大部分都是基于国外的研究 ...
- 20144303石宇森《网络对抗》注入shellcode和Return-to-libc攻击
20144303石宇森<网络对抗>PC平台逆向破解 实验1:shellcode注入 实验基础 1.Linux下有两种基本构造攻击buf的方法:retaddr+nop+shellcode,n ...
- tf.reduce_sum tensorflow维度上的操作
tensorflow中有很多在维度上的操作,本例以常用的tf.reduce_sum进行说明.官方给的api reduce_sum( input_tensor, axis=None, keep_dims ...
- noip2008 真题练习 2017.2.25
不是有很多可以说的,记住不能边算边取min Code #include<iostream> #include<fstream> #include<sstream> ...
- HTML基本格式
<html> <head> <title>放置文章标题</title> <meta http-equiv="Content-Type&q ...
- C# 将 Stream 写入文件
public void StreamToFile(Stream stream,string fileName) { // 把 Stream 转换成 byte[] byte[] bytes = new ...
- C# 判断网络文件是否存在
#region 判断网络文件是否存在 //bool result = false;//下载结果 //string fileUrl = "http://localhost:7070/FileU ...