problem

C. Parsa’s Humongous Tree
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Parsa has a humongous tree on n vertices.

On each vertex v he has written two integers lv and rv.

To make Parsa’s tree look even more majestic, Nima wants to assign a number av (lv≤av≤rv) to each vertex v such that the beauty of Parsa’s tree is maximized.

Nima’s sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of |au−av| over all edges (u,v) of the tree.

Since Parsa’s tree is too large, Nima can’t maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa’s tree.

Input
The first line contains an integer t (1≤t≤250) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (2≤n≤105) — the number of vertices in Parsa’s tree.

The i-th of the following n lines contains two integers li and ri (1≤li≤ri≤109).

Each of the next n−1 lines contains two integers u and v (1≤u,v≤n,u≠v) meaning that there is an edge between the vertices u and v in Parsa’s tree.

It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of n over all test cases doesn’t exceed 2⋅105.

Output
For each test case print the maximum possible beauty for Parsa’s tree.

Example
inputCopy
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
outputCopy
7
8
62
Note
The trees in the example:

In the first test case, one possible assignment is a={1,8} which results in |1−8|=7.

In the second test case, one of the possible assignments is a={1,5,9} which results in a beauty of |1−5|+|5−9|=8

solution

//求相邻点的最大绝对值之差,那么肯定是取两个点的左端点和右端点最优
//f[x][0]表示ax=l时,x的子树的美丽值之和,f[x][1]表示ax=r
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
vector<int>G[maxn];
LL l[maxn], r[maxn], f[maxn][2];
void dfs(int u, int fa){for(int v: G[u]){if(v==fa)continue;dfs(v,u);f[u][0] += max(f[v][0]+abs(l[u]-l[v]), f[v][1]+abs(l[u]-r[v]));f[u][1] += max(f[v][0]+abs(r[u]-l[v]), f[v][1]+abs(r[u]-r[v]));}
}
int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;  cin>>T;while(T--){int n;  cin>>n;for(int i = 1; i <= n; i++){cin>>l[i]>>r[i];f[i][0] = f[i][1] = 0;G[i].clear();}for(int i = 1; i <= n-1; i++){int u, v;  cin>>u>>v;G[u].push_back(v);G[v].push_back(u);}dfs(1,0);LL ans = 0;for(int i = 1; i <= n; i++)ans = max(ans, max(f[i][0],f[i][1]) );cout<<ans<<"\n";}return 0;
}

CF Round #722 (Div. 2) C. Parsa‘s Humongous Tree(树形dp)相关推荐

  1. A. Parsa‘s Humongous Tree(树形DP + 贪心)

    Problem - 1528A - Codeforces 两个玩家正在玩一个游戏.他们有一个整数1,2,...,n的排列组合(排列组合是一个数组,其中从1到n的每个元素正好出现一次).这个排列组合没有 ...

  2. 思维dp ---- Codeforces Round #722 (Div. 1) B. Kavi on Pairing Duty [思维dp + 数学]

    题目大意: 将2n2n2n个点两两相连形成n对,对于任意两个点对A和B,要求至少满足其中一条: 1.A和B的某一个完全包含于另一个中 2.A和B的长度相等.问你一共有多少种方案. 解题思路: 题解:假 ...

  3. Codeforces Round #722 (Div. 2)

    Codeforces Round #722 (Div. 2) 题号 题目 知识点 A Eshag Loves Big Arrays(题解略) 贪心 B Sifid and Strange Subseq ...

  4. CF 1529C Parsa‘s Humongous Tree

    CF 1529C Parsa's Humongous Tree 题意: 给你一颗n个点,n-1个边的树,每个点的点权为一个区间值,树的值为边权和. 边权为该边的两个端点的点权差的绝对值的和 问树的值最 ...

  5. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  6. Codeforces Round #579 (Div. 3) F2. Complete the Projects (hard version) dp + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 排序方式跟easyeasyeasy版本的一样,但是hardhardhard版本是输出最多能选多少,所以我们对b<0b<0b<0的情况 ...

  7. Codeforces Round #590 (Div. 3) F. Yet Another Substring Reverse 子集dp

    传送门 文章目录 题意: 思路: 题意: 思路: 之前做过类似的题,翻转一个字串相当于将任意两个不相交的串连在一起.再一看字符集≤20\le20≤20,那就是铁子集dpdpdp了. 定义f[i]f[i ...

  8. Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp + 输出方案

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的序列aaa,你需要将其分成若干组,每组的价值为max⁡(ai)−min(ai)\max(a_i)-min(a_i)max(ai​)−mi ...

  9. Codeforces Round #599 (Div. 2) E. Sum Balance 图转换 + 子集dp + 环

    传送门 文章目录 题意: 思路: 题意: 思路: 首先我们知道如果所有数的和summodk!=0sum\bmod k!=0summodk!=0那么此时无解,否则我们设need=sum/kneed=su ...

最新文章

  1. 对话框处理与登录测试
  2. java集合框架图(一)
  3. 三值的排序 Sorting a Three-Valued Sequence
  4. c++引用另一个类的方法_利用CVE20191132:Windows内核中的另一个NULL指针取消引用...
  5. outlook不能保存密码_教大家轻松保存Outlook当中的附件到Onedrive文件夹上
  6. Android阅读手札:第一行代码(第一章)
  7. SQL系列(二)—— 查询(select)
  8. C++程序中调用exe可执行文件
  9. opencv java 人脸识别_Java OpenCV实现人脸识别过程详解
  10. MOSSE相关滤波目标跟踪论文
  11. 降低软件购置成本 实现系统集中部署 ——沟通CTBS平台上海工化院应用案例
  12. 用计算机求回归,鲜为人知的用途,回归分析用计算器就能做!
  13. 小程序 — 关于图片Base64转换及空间大小问题
  14. 云计算基础与应用 第七章 CDN
  15. 华为担纲建设基础软硬件国家新一代AI开放创新平台
  16. CJT长江连接器A2005系列线对板连接器排针排母PCB封装库
  17. 可以在PowerShell里使用的Word度量单位
  18. python 股票竞价数据_GitHub - TruthHun/auction-stock: 集合竞价选股(股票),基于收盘价与前收盘价的选股策略...
  19. JSON格式转MAP的6种方法
  20. 杰理之IIC及其他配置定义

热门文章

  1. bsxfun 的理解
  2. python实用技巧(二)
  3. Python 标准库 —— os 模块
  4. html高级编辑工具,高级编辑工具
  5. euv光刻机有什么用_有关EUV光刻机,你需要知道这些
  6. c++画函数图像_二次函数图像与系数a,b,c的关系
  7. python难学吗-Python为什么那么受欢迎?学习Python难不难?
  8. python发音语言-python 利用pyttsx3文字转语音过程详解
  9. python和java哪个好-python和java哪个更强大?
  10. 怎么让某段css代码只在Chrome 火狐 edge 浏览器生效