Wednesday, July 13, 2022

Chef and Minimum Cost of His Tree

 You are a given a tree of 

N vertices. The i-th vertex has two things assigned to it — a value Ai and a range [Li,Ri]. It is guaranteed that LiAiRi.

You perform the following operation exactly once:

  • Pick a (possibly empty) subset S of vertices of the tree. S must satisfy the additional condition that no two vertices in S are directly connected by an edge, i.e, it is an independent set.
  • Then, for each xS, you can set Ax to any value in the range [Lx,Rx] (endpoints included).

Your task is to minimize the value of |AuAv| over all unordered pairs (u,v) such that u and v are connected by an edge.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains a single integer N, the number of vertices.
    • The next N lines describe the values of the vertices. The i-th of these N lines contains three space-separated integers LiAi, and Ri respectively
    • The next N1 lines describe the edges. The i-th of these N1 lines contains two space-separated integers ui and vi, denoting an edge between ui and vi.

Output Format

For each test case, output on a new line the minimum possible value as described in the statement.

Constraints

  • 1T104
  • 3N105
  • 1ui,viN
  • uivi for each 1iN1
  • 1LiAiRi109
  • The sum of N over all test cases won't exceed 2105.

Sample Input 1 

2
3
1 1 1
2 3 3
3 3 3
1 2
1 3
4
3 3 4
1 3 5
2 2 2
2 2 2
1 2
2 3
2 4

Sample Output 1 

3
1

Explanation

Test case 1: Set the value of A2 to 2. The answer is |A1A2|+|A1A3|=|12|+|13|=3.

Test case 2: Set the value of A2 to 2. The answer is .

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...