Problem Description
Problem Analysis
Note that a value of 0 does not necessarily mean it is really 0
rather, the given data simply hasn’t been checked against it
So the Template needs the following changes:
When find locates the root, it must compute and store the sum from 1 to itself
let its own value add the parent’s value
and in this process, through Recursion
make the parent’s value add its own parent’s value
while also performing path compression
so that it connects directly to the root
since the root has no data before it to become its parent
sum[root] should always be 0
thus, while finding the root, the computation of the interval value from itself to the root is completed
For the unite merge operation
the idea is
if the two numbers have the same root, then check
otherwise compute the sum interval according to the size of the two roots
then merge according to the relation of the two numbers
Because sum stores its value to the root
the order of a, b is determined, and the assignment follows
(It seems there are other approaches, but I think the more complex the problem, the more ways there sometimes are to solve it, so I won’t force myself to fully understand everyone else’s other approaches)
AC code:
note that there are multiple test cases
| |
Problem link: 【杭电】[3038]How Many Answers Are Wrong