| INOI 2024 |
|---|
| Finished |
A tree is a set of $$$N$$$ nodes and $$$N-1$$$ undirected edges. Each edge connects two distinct nodes, and between any two nodes there is a unique path that doesn't repeat edges. Further, each edge has an integer edge weight.
You are given two trees, tree X and tree Y. In both these trees, there are $$$N$$$ nodes numbered from $$$1$$$ to $$$N$$$ and $$$N-1$$$ edges numbered from $$$1$$$ to $$$N-1$$$, but the specific edge structures and edge weights of tree X and tree Y may differ.
In tree X, the $$$i$$$-th edge connects nodes $$$A_i$$$ and $$$B_i$$$ and has weight $$$C_i$$$.
In tree Y, the $$$j$$$-th edge connects nodes $$$U_j$$$ and $$$V_j$$$ and has weight $$$W_j$$$.
For some two nodes $$$p$$$ and $$$q$$$ such that $$$1 \leq p \lt q \leq N$$$, $$$\text{cost}_X(p, q)$$$ is defined as the largest edge weight that occurs on the unique path between $$$p$$$ and $$$q$$$ in tree X. Similarly, $$$\text{cost}_Y(p, q)$$$ is defined as the largest edge weight that occurs on the unique path between $$$p$$$ and $$$q$$$ in tree Y.
You are asked to find the number of pairs of nodes $$$(p, q)$$$ such that $$$1 \leq p \lt q \leq N$$$ and $$$\text{cost}_X(p, q) \leq \text{cost}_Y(p, q)$$$. (In total, there are $$$\frac{N(N-1)}{2}$$$ pairs of nodes $$$(p, q)$$$ such that $$$1 \leq p \lt q \leq N$$$.)
The first line of input contains a single integer $$$N$$$, the number of nodes.
The next $$$N-1$$$ lines describe tree X. The $$$i$$$-th of these lines contains three integers, $$$A_i$$$, $$$B_i$$$, and $$$C_i$$$.
The next $$$N-1$$$ lines describe tree Y. The $$$j$$$-th of these lines contains three integers, $$$U_j$$$, $$$V_j$$$, and $$$W_j$$$.
You should print a single integer, the number of pairs of nodes $$$(p, q)$$$ such that $$$1 \leq p \lt q \leq N$$$ and $$$\text{cost}_X(p, q) \leq \text{cost}_Y(p, q)$$$.
The test data for this problem is divided into multiple subtasks. In order to pass a subtask, your submitted program must solve every test case within that subtask correctly and within the time and memory limits.
You will be awarded the points allocated to a subtask if at least one submission you make during the contest passes that subtask. You do not need to combine your solutions for multiple subtasks into a single submission.
Please keep in mind that the subtasks are not necessarily arranged in increasing order of difficulty. We encourage you to try as many subtasks as possible.
Constraints
In all test data, it is guaranteed that:
Please be aware that the output for this problem may not fit in 32-bit integers. You may need to use 64-bit integers in your computations.
Notation
The following notation is used to describe the subtasks:
Subtasks
41 2 22 3 33 4 91 2 22 3 63 4 8
3
51 2 82 3 63 4 24 5 71 2 102 3 43 4 74 5 5
8
51 4 62 3 81 2 92 5 101 4 82 3 81 2 82 5 8
2
61 2 22 4 63 2 84 5 81 6 101 2 32 4 53 2 54 5 91 6 11
10
81 2 62 3 61 4 102 5 74 6 73 7 15 8 41 2 21 3 61 4 24 5 91 6 25 7 17 8 1
11
In the first sample :
We can show that the pairs $$$(p, q) = (1, 2), (2, 3), (1, 3)$$$ all satisfy $$$\text{cost}_X(p, q) \leq \text{cost}_Y(p, q)$$$ while the other $$$3$$$ pairs do not. Hence, the answer is $$$3$$$.
Sample $$$1$$$ is valid for Subtasks 1, 2, 3, 4, 5, 7, 8.
Sample $$$2$$$ is valid for Subtasks 2, 3, 5, 8.
Sample $$$3$$$ is valid for Subtasks 2, 3, 6, 7, 8.
Sample $$$4$$$ is valid for Subtasks 2, 3, 7, 8.
Sample $$$5$$$ is valid for Subtasks 2, 3, 8.
| Name |
|---|


