1528A.md (2037B)
1 Source: https://codeforces.com/problemset/problem/1528/A 2 3 A. Parsa's Humongous Tree 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Parsa has a humongous tree on $n$ vertices. 22 23 On each vertex $v$ he has written two integers $l_v$ and $r_v$. 24 25 To make Parsa's tree look even more majestic, Nima wants to assign a number $a_v$ ($l_v \le a_v \le r_v$) to each vertex $v$ such that the beauty of Parsa's tree is maximized. 26 27 Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $|a_u - a_v|$ over all edges $(u, v)$ of the tree. 28 29 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. 30 31 Input 32 33 The first line contains an integer $t$ $(1\le t\le 250)$ — the number of test cases. The description of the test cases follows. 34 35 The first line of each test case contains a single integer $n$ $(2\le n\le 10^5)$ — the number of vertices in Parsa's tree. 36 37 The $i$-th of the following $n$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le 10^9)$. 38 39 Each of the next $n-1$ lines contains two integers $u$ and $v$ $(1 \le u , v \le n, u\neq v)$ meaning that there is an edge between the vertices $u$ and $v$ in Parsa's tree. 40 41 It is guaranteed that the given graph is a tree. 42 43 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. 44 45 Output 46 47 For each test case print the maximum possible beauty for Parsa's tree. 48 49 Example 50 51 Input 52 53 ``` 54 3 55 2 56 1 6 57 3 8 58 1 2 59 3 60 1 3 61 4 6 62 7 9 63 1 2 64 2 3 65 6 66 3 14 67 12 20 68 12 19 69 2 12 70 10 17 71 3 17 72 3 2 73 6 5 74 1 5 75 2 6 76 4 6 77 ``` 78 79 Output 80 81 ``` 82 7 83 8 84 62 85 ``` 86 87 Note 88 89 The trees in the example: 90 91  92 93 In the first test case, one possible assignment is $a = \{1, 8\}$ which results in $|1 - 8| = 7$. 94 95 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$