Codeforces 915F Imbalance Value of a Tree
感觉这种题没啥营养, 排个序算算贡献就好啦。
- #include<bits/stdc++.h>
- #define LL long long
- #define fi first
- #define se second
- #define mk make_pair
- #define PLL pair<LL, LL>
- #define PLI pair<LL, int>
- #define PII pair<int, int>
- #define SZ(x) ((int)x.size())
- #define ull unsigned long long
- using namespace std;
- const int N = 1e6 + ;
- const int inf = 0x3f3f3f3f;
- const LL INF = 0x3f3f3f3f3f3f3f3f;
- const int mod = 1e9 + ;
- const double eps = 1e-;
- const double PI = acos(-);
- int n, id[N], a[N], fa[N], cnt[N];
- LL ans;
- vector<int> G[N];
- int getRoot(int x) {
- return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
- }
- bool cmp(const int& x, const int& y) {
- return a[x] < a[y];
- }
- int main() {
- scanf("%d", &n);
- for(int i = ; i <= n; i++) {
- scanf("%d", &a[i]);
- id[i] = i;
- }
- for(int i = ; i <= n; i++) {
- int u, v; scanf("%d%d", &u, &v);
- G[u].push_back(v);
- G[v].push_back(u);
- }
- sort(id + , id + + n, cmp);
- for(int i = ; i <= n; i++) fa[i] = i, cnt[i] = ;
- for(int i = ; i <= n; i++) {
- int u = id[i];
- for(auto& v : G[u]) {
- if(a[u] > a[v] || (a[u] == a[v] && u > v)) {
- int x = getRoot(u);
- int y = getRoot(v);
- ans += 1ll * a[u] * cnt[x] * cnt[y];
- fa[y] = x;
- cnt[x] += cnt[y];
- }
- }
- }
- reverse(id + , id + + n);
- for(int i = ; i <= n; i++) fa[i] = i, cnt[i] = ;
- for(int i = ; i <= n; i++) {
- int u = id[i];
- for(auto& v : G[u]) {
- if(a[u] < a[v] || (a[u] == a[v] && u > v)) {
- int x = getRoot(u);
- int y = getRoot(v);
- ans -= 1ll * a[u] * cnt[x] * cnt[y];
- fa[y] = x;
- cnt[x] += cnt[y];
- }
- }
- }
- printf("%lld\n", ans);
- return ;
- }
- /*
- */
Codeforces 915F Imbalance Value of a Tree的更多相关文章
- 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 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
- codeforces 1065F Up and Down the Tree
题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- [CF915F]Imbalance Value of a Tree
[CF915F]Imbalance Value of a Tree 题目大意: 一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\).定义\(I(i,j)\)为\(i\)到\ ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Imbalance Value of a Tree CodeForces - 915F
链接 大意: 给定树, 求树上所有链上最大值最小值之差 817D的树上版本, 用并查集维护即可. 817D由于是链的情况并查集不必压缩路径即可达到均摊$O(n)$, 该题必须压缩, 复杂度$O(nlo ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
随机推荐
- log4j入门
日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...
- string和list互转
import string str = 'abcde' list = list(str) OR list = str.split() list ['a', 'b', 'c', 'd', 'e'] st ...
- [洛谷P3948]数据结构 题解(差分)
[洛谷P3948]数据结构 Description 最开始的数组每个元素都是0 给出n,opt ,min,max,mod 在int范围内 A: L ,R ,X 表示把[l,R] 这个区间加上X(数组的 ...
- BZOJ 4614[Wf2016]Oil
权限题鸭qwq 首先可以知道最优答案选出来的直线一定可以经过某条线段左端点,如果这条直线没有过左端点,可以通过平移和旋转等操作达到.所以可以枚举这条直线过了哪条线段的左端点,那么对于其他线段,能对答案 ...
- composer设计原理与基本用法
原文地址:http://blog.turn.tw/?p=1039 COMPOSER進階原理:PHP命名空間與PSR-0 http://blog.turn.tw/?p=1122 Moving PHP ...
- Fragment与Activity之间的相互通信
https://blog.csdn.net/u012702547/article/details/49786417 https://blog.csdn.net/carson_ho/article/de ...
- 2017-2018-2 20155303 『网络对抗技术』Exp3:免杀原理与实践
2017-2018-2 20155303 『网络对抗技术』Exp3:免杀原理与实践 --------CONTENTS-------- 1. 免杀原理与实践说明 实验说明 基础问题回答 2. 使用msf ...
- python内置模块之collections(六)
前言 collections是Python内建的一个集合模块,提供了许多有用的集合类. 系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python ...
- 为caffe添加最简单的全通层AllPassLayer
参考赵永科的博客,这里我们实现一个新 Layer,名称为 AllPassLayer,顾名思义就是全通 Layer,“全通”借鉴于信号处理中的全通滤波器,将信号无失真地从输入转到输出. 虽然这个 Lay ...
- jdk8系列三、jdk8之stream原理及流创建、排序、转换等处理
一.为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX ...