Test instructions: An enterprise is going to buy some companies to make the interconnection between each enterprise, just start each company independent of each other
Given n companies, two operations
E I: Ask I to I it connection point the distance of the last company
I i J: Will I point to J Company, that is, J company is I Company's superior, distance for abs (I-J)%1000 (seemingly g++ not support abs,pe two rounds)
Idea: Conversion of test instructions on the line, first of all, the beginning of each company is independent, I operation is and check the merger operation, I this lesson tree and to J this tree,
E Operation requirements of the thing is I to I of the root node distance, first look at a no path compression direct violence method, (I thought not, because the data is very large)
Time is: 2222ms was despised by the system
And so on, now the examples given in the topic to direct the implementation of the I operation, and finally get the 3->1->2->4
So the result of E 3 is (3-1)%1000+ (2-1)%1000+ (4-2)%1000=5
And the check set itself is such a result, so directly from the child node to the root node to find the past, while looking for the distance
Path compression version, 66MS
UVA 3027 corporative Network with right and check set,