Imbalance Value of a Tree

感觉这种题没啥营养, 排个序算算贡献就好啦。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define PLL pair<LL, LL>
  7. #define PLI pair<LL, int>
  8. #define PII pair<int, int>
  9. #define SZ(x) ((int)x.size())
  10. #define ull unsigned long long
  11.  
  12. using namespace std;
  13.  
  14. const int N = 1e6 + ;
  15. const int inf = 0x3f3f3f3f;
  16. const LL INF = 0x3f3f3f3f3f3f3f3f;
  17. const int mod = 1e9 + ;
  18. const double eps = 1e-;
  19. const double PI = acos(-);
  20.  
  21. int n, id[N], a[N], fa[N], cnt[N];
  22. LL ans;
  23. vector<int> G[N];
  24.  
  25. int getRoot(int x) {
  26. return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
  27. }
  28.  
  29. bool cmp(const int& x, const int& y) {
  30. return a[x] < a[y];
  31. }
  32.  
  33. int main() {
  34. scanf("%d", &n);
  35. for(int i = ; i <= n; i++) {
  36. scanf("%d", &a[i]);
  37. id[i] = i;
  38. }
  39. for(int i = ; i <= n; i++) {
  40. int u, v; scanf("%d%d", &u, &v);
  41. G[u].push_back(v);
  42. G[v].push_back(u);
  43. }
  44. sort(id + , id + + n, cmp);
  45. for(int i = ; i <= n; i++) fa[i] = i, cnt[i] = ;
  46. for(int i = ; i <= n; i++) {
  47. int u = id[i];
  48. for(auto& v : G[u]) {
  49. if(a[u] > a[v] || (a[u] == a[v] && u > v)) {
  50. int x = getRoot(u);
  51. int y = getRoot(v);
  52. ans += 1ll * a[u] * cnt[x] * cnt[y];
  53. fa[y] = x;
  54. cnt[x] += cnt[y];
  55. }
  56. }
  57. }
  58. reverse(id + , id + + n);
  59. for(int i = ; i <= n; i++) fa[i] = i, cnt[i] = ;
  60. for(int i = ; i <= n; i++) {
  61. int u = id[i];
  62. for(auto& v : G[u]) {
  63. if(a[u] < a[v] || (a[u] == a[v] && u > v)) {
  64. int x = getRoot(u);
  65. int y = getRoot(v);
  66. ans -= 1ll * a[u] * cnt[x] * cnt[y];
  67. fa[y] = x;
  68. cnt[x] += cnt[y];
  69. }
  70. }
  71. }
  72. printf("%lld\n", ans);
  73. return ;
  74. }
  75.  
  76. /*
  77. */

Codeforces 915F Imbalance Value of a Tree的更多相关文章

  1. Codeforces 915F Imbalance Value of a Tree(并查集)

    题目链接  Imbalance Value of a Tree 题意  给定一棵树.求树上所有简单路径中的最大权值与最小权值的差值的和. 首先考虑求所有简单路径中的最大权值和. 对所有点按照权值大小升 ...

  2. 【CodeForces】915 F. Imbalance Value of a Tree 并查集

    [题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...

  3. Codeforces 915 F. Imbalance Value of a Tree(并查集)

    F. Imbalance Value of a Tree 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...

  4. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...

  5. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...

  6. [CF915F]Imbalance Value of a Tree

    [CF915F]Imbalance Value of a Tree 题目大意: 一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\).定义\(I(i,j)\)为\(i\)到\ ...

  7. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  8. Imbalance Value of a Tree CodeForces - 915F

    链接 大意: 给定树, 求树上所有链上最大值最小值之差 817D的树上版本, 用并查集维护即可. 817D由于是链的情况并查集不必压缩路径即可达到均摊$O(n)$, 该题必须压缩, 复杂度$O(nlo ...

  9. 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 ...

随机推荐

  1. log4j入门

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...

  2. string和list互转

    import string str = 'abcde' list = list(str) OR list = str.split() list ['a', 'b', 'c', 'd', 'e'] st ...

  3. [洛谷P3948]数据结构 题解(差分)

    [洛谷P3948]数据结构 Description 最开始的数组每个元素都是0 给出n,opt ,min,max,mod 在int范围内 A: L ,R ,X 表示把[l,R] 这个区间加上X(数组的 ...

  4. BZOJ 4614[Wf2016]Oil

    权限题鸭qwq 首先可以知道最优答案选出来的直线一定可以经过某条线段左端点,如果这条直线没有过左端点,可以通过平移和旋转等操作达到.所以可以枚举这条直线过了哪条线段的左端点,那么对于其他线段,能对答案 ...

  5. composer设计原理与基本用法

    原文地址:http://blog.turn.tw/?p=1039 COMPOSER進階原理:PHP命名空間與PSR-0   http://blog.turn.tw/?p=1122 Moving PHP ...

  6. Fragment与Activity之间的相互通信

    https://blog.csdn.net/u012702547/article/details/49786417 https://blog.csdn.net/carson_ho/article/de ...

  7. 2017-2018-2 20155303 『网络对抗技术』Exp3:免杀原理与实践

    2017-2018-2 20155303 『网络对抗技术』Exp3:免杀原理与实践 --------CONTENTS-------- 1. 免杀原理与实践说明 实验说明 基础问题回答 2. 使用msf ...

  8. python内置模块之collections(六)

    前言 collections是Python内建的一个集合模块,提供了许多有用的集合类. 系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python ...

  9. 为caffe添加最简单的全通层AllPassLayer

    参考赵永科的博客,这里我们实现一个新 Layer,名称为 AllPassLayer,顾名思义就是全通 Layer,“全通”借鉴于信号处理中的全通滤波器,将信号无失真地从输入转到输出. 虽然这个 Lay ...

  10. jdk8系列三、jdk8之stream原理及流创建、排序、转换等处理

    一.为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX ...