Codeforces 915F Imbalance Value of a Tree(并查集)
题目链接 Imbalance Value of a Tree
题意 给定一棵树。求树上所有简单路径中的最大权值与最小权值的差值的和。
首先考虑求所有简单路径中的最大权值和。
对所有点按照权值大小升序排序,即若$a[i] < a[j]$,那么$i$排在$j$前面。
接下来开始依次处理。对于每个点$i$,寻找周围跟他连通并且权值比他小的点进行合并,并且累加答案。
整个过程用并查集维护。
那么类似地,求最小权值的和的时候,我们把所有点权变成原来的相反数,再做一遍即可。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 1e6 + 10; int a[N];
int sz[N];
int n;
int father[N];
int c[N];
LL ans = 0;
vector <int> v[N]; int getfather(int x){
return father[x] == x ? x : father[x] = getfather(father[x]);
} bool cmp(const int &x, const int &y){
return a[x] < a[y];
} void work(int x, int y, int z){
int fx = getfather(x);
int fy = getfather(y);
if (!fx || !fy) return;
ans += 1ll * z * sz[fx] * sz[fy];
father[fx] = fy;
sz[fy] += sz[fx];
sz[fx] = 0;
} void solve(){
rep(i, 1, n) c[i] = i;
sort(c + 1, c + n + 1, cmp); rep(i, 1, n) father[i] = 0, sz[i] = 0;
rep(i, 1, n){
int x = c[i];
father[x] = x;
sz[x] = 1;
for (auto u : v[x]){
work(x, u, a[x]);
}
}
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i);
rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} solve();
rep(i, 1, n) a[i] *= -1;
solve();
printf("%lld\n", ans);
return 0;
}
Codeforces 915F Imbalance Value of a Tree(并查集)的更多相关文章
- 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 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...
- 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 Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
随机推荐
- 手机注册过哪些网站37kfenxi.com,查询注册过哪些网站
注册过哪些网站?发现这么一个网站,https://www.37kfenxi.com?_=cnblogs 可以根据手机号码查询注册过哪些网站,然后通过大数据分析出机主的性格,爱好等. 据说还可以查老板, ...
- linux学习(二) -- ubuntu下lnmp环境的配置
亲测的教程,,希望能对大家提供些许帮助,转载请注明出处 ubuntu+nginx+mysql+php7 一.安装Nginx 1.首先添加nginx_signing.key(必须,否则出错) $ wge ...
- leetcode 【 Majority Element 】python 实现
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- linux环境搭建系列之Apache ant安装步骤
前提: linux centOS6.6 64位操作系统 ROOT账号 JDK1.7 注意:JDK和Ant版本一定要是对应的,因为我的JDK是1.7版本的,所以我在官网下载的ant是1.9的 1.把安装 ...
- 非旋Treap总结 : 快过Splay 好用过传统Treap
非旋$Treap$ 其高级名字叫$Fhq\ Treap$,既然叫$Treap$,它一定满足了$Treap$的性质(虽然可能来看这篇的人一定知道$Treap$,但我还是多说几句:$Fhp\ Treap$ ...
- 二分法 Binary Search
二分法还是比较常见和简单的,之前也遇到过一些二分的相关题目,虽然不难,但是每次都需要在边界问题上诸多考虑,今天听了九章算法的课程,学习到一种方法使得边界问题简单化. 二分法的几个注意点: 1. mid ...
- webform登陆界面样式丢失
本文摘抄自:http://blog.csdn.net/sssix/article/details/16945347 请阅读原文. Forms验证——登录界面样式实效? <authenticati ...
- java替换富文本标签等
(转自:http://www.cnblogs.com/1246447850qqcom/p/5439366.html) package q;import java.util.regex.Matcher; ...
- 【bzoj2738】矩阵乘法 整体二分+二维树状数组
题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5个数 ...
- 刷题总结——次小生成树(bzoj1977 最小生成树+倍增)
题目: Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C ...