I was trying to solve this problem and this is my AC code. I then found This short solution written by some user. Can anyone explain what it does and why is it correct?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I was trying to solve this problem and this is my AC code. I then found This short solution written by some user. Can anyone explain what it does and why is it correct?
Name |
---|
Start at any node, do dfs to find the farthest node from it, then do dfs on that node, calculate distances for every node from that node, then do dfs again, but on node that was farthest in second dfs, calculate distances again, and max distance for any node is max of 2 distances you calculated in dfs for that node.
and why does it work ? if I understood well d1,d2 save the distances from two leaves of the diameter to every node?
Auto comment: topic has been updated by youssefbou62 (previous revision, new revision, compare).
The solution relies on finding a diameter, and then using the fact that the maximum distance from any node always goes to one of the endpoints of that diameter. The proof I found has a few cases, but is fairly straightforward if you draw the cases and trace the distances.
I write x-y for the distance between node x and node y. Let a-d be a diameter (so the maximum distance between two nodes). Now assume we want to find the furthest node from x. Take some arbitrary node y, we want to prove x-y $$$\le$$$ max(x-a,x-d). If this is true for all nodes y, then max(x-a,x-d) is the largest distance from node x.
Let x be connected to the diameter at node b, and y be connected to the diameter at node c. Since a-d is the maximum distance between any two nodes (the diameter), b-x $$$\le$$$ min(a-b,b-d), otherwise x-a or x-d would be longer than the diameter. Similarly, c-y $$$\le$$$ min(a-c,c-d).
If b = c (so the path from x to y is not part of the diameter), then:
x-y $$$\le$$$ x-b + c-y $$$\le$$$ x-b + min(a-c,c-d) $$$\le$$$ x-b + c-d $$$\le$$$ max(x-a,x-d).
Otherwise, if a,b,c,d are in that order:
x-y = x-b + b-c + c-y $$$\le$$$ x-b + b-c + min(a-c,c-d) $$$\le$$$ x-b + b-c + c-d = x-d $$$\le$$$ max(x-a,x-d).
Similarly, if the order is a,c,b,d:
x-y = x-b + b-c + c-y $$$\le$$$ x-b + b-c + min(a-c,c-d) $$$\le$$$ x-b + b-c + a-c = x-a $$$\le$$$ max(x-a,x-d).
So in conclusion, for any nodes x and y, x-y $$$\le$$$ max(x-a,x-d), which is what the code computes.
To find the endpoints of a diameter, the code just takes the furthest point from any starting point, twice. It uses the fact that the furthest point from any point x, is an endpoint of a diameter. The proof of this is similar to the above, and can be thought of as proving that x-y = max(x-a,x-d) if and only if y-a or y-b is also a diameter.
Please correct me if you find mistakes.
Got it , thanks a lot !