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. FastReport.Net报表故障排除方法

    有不少开发人员在使用fastreport报表时遇到过这样的问题,报表设计器工作时,一些工具栏或者工具窗口被损坏了.此时,你应该删除配置文件,该文件是在你启动fastreport时创建的.它位于以下文件 ...

  2. VS2017的安装和配置

    VS2017专业版安装文件下载:链接:https://pan.baidu.com/s/1tJRYdj_9LzvTSDF5TkkMRg   提取码:tgh5    一些窗口:菜单栏--->视图 字 ...

  3. luogu P2303 [SDOi2012]Longge的问题

    传送门 \[\sum_{i=1}^{n}\gcd(i,n)\] 考虑枚举所有可能的gcd,可以发现这一定是\(n\)的约数,当\(\gcd(i,n)=x\)时,\(gcd(\frac{i}{x},\f ...

  4. 第18月第10天 iOS11 uicollectionview

    1. - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollect ...

  5. Problem F Plug It In!

    题目链接:https://cn.vjudge.net/contest/245468#problem/F 大意:给你插座和电器的对应关系,有多个电器对应一个插座的情况,但是一个插座只能供一个电器使用,现 ...

  6. python - 数据描述符(class 内置 get/set/delete方法 )

    数据描述符(class 内置 get/set/del方法 ): # 什么是描述符 # 官方的定义:描述符是一种具有“捆绑行为”的对象属性.访问(获取.设置和删除)它的属性时,实际是调用特殊的方法(_g ...

  7. 单页应用 WebApp SPA 骨架 框架 路由 页面切换 转场

    这里收录三个同类产品,找到他们花了我不少时间呢. 张鑫旭写的mobilebone自述:mobile移动端,PC桌面端页面无刷新过场JS骨架,简单.专注!http://www.zhangxinxu.co ...

  8. js实现页面遮罩层,并且阻止页面body滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【转载】Hadoop mapreduce 实现原理

    1.  如何用通俗的方法解释MapReduce MapReduce是Google开源的三大技术之一,是对海量数据进行“分而治之”计算框架.为了简单的理解并讲述给客户理解.我们举下面的例子来说明. 首先 ...

  10. sublime3添加python编译系统

    好记性不如烂笔头 为sublime3添加python编译系统,这里使用的anonconda2中的python.exe(即python2.7版本) 步骤: (1)打开sublime,打开“工具-> ...