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的更多相关文章

  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. Oracle——存储过程简单入门实例

    1.连接plsql developer,打开一个SQL Window 2.SQL Window中创建表user_info -- Create table create table USER_INFO ...

  2. Anaconda的安装和更新

    下载地址官网:https://www.anaconda.com/distribution/ 一.安装 二.下载安装完成后我们来检验一下是否安装成功 点击“开始” —— “Anaconda3(64-bi ...

  3. Android学习笔记——Bluetooth的使用

    蓝牙应该是现在每一部智能手机的标配了.想当年在山寨机横行的年代里,蓝牙都可以做为一个卖点~~~ 废话不多说了,进入正题: 使用蓝牙功能是需要权限的,关于蓝牙的权限也就两个: <uses-perm ...

  4. js 执行上下文理解

    前端基础进阶(三):变量对象详解http://www.jianshu.com/p/330b1505e41d 1.创建阶段 a.生成变量对象    1.创建arguments对象   2.functio ...

  5. Your Database is downloaded and backed up on....(腾讯云的mysql被攻击)

    今天发现自己的服务器被黑客攻击,自己的mysql服务器的库被删掉,并且新创了一个warning库,只有一个readme表.不知道原因,也许是自己再github上的项目暴漏了自己的密码,还要0.6比特币 ...

  6. ROS 时间同步问题

    0. 问题 两台ubuntu主机无法与一台debian主机使用分布式通信,摄像头发出的话题机器人收不到,考虑是时间同步的问题. 也可能是系统不统一的问题; 今天在家实验了一下,时间差6min,照样可以 ...

  7. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  8. (并发编程)进程IPC,生产者消费者模型,守护进程补充

    一.IPC(进程间通信)机制进程之间通信必须找到一种介质,该介质必须满足1.是所有进程共享的2.必须是内存空间附加:帮我们自动处理好锁的问题 a.from multiprocessing import ...

  9. MariaDB基于GTID主从复制及多主复制

    一.简单主从模式配置步骤(必须要mysql5.6,此处以maridb10.0.10为例) 1.配置主从节点的服务配置文件 # vim /etc/my.cnf 1.1.配置master节点: [mysq ...

  10. S5PV210初始化系统时钟

    S5PV210初始化系统时钟 S5PV210时钟体系S5PV210中包含3大类时钟domain,分别是主系统时钟domain (简称MSYS,下面将使用简称来进行相关讲解).显示相关的时钟domain ...