Tree Reconstruction UVA - 10410

This problem is true. . will not. I doubt that the two main points of diversity relations dfs number of adjacent, so I always wanted to enumerate the middle of the storm to search, find not write code. Then it must be noted that traverse from small to large, and would not use this property. . In short attitude is completely out of the collapse, mainly too much food.

Title Description: https://vjudge.net/problem/UVA-10410

Internet a more detailed explanation: https://www.cnblogs.com/jerryRey/p/4622927.html

The core idea is mainly in the case of the equivalent of it, in order dfs and bfs sequence simultaneously in adjacent, brotherhood and parent-child relationships are equivalent, then we can traverse through the ascending order of judge relations.

 

dfs and bfs is defined as the position in the sequence.

Set the current node u, is the next node v. Relationship is as follows:

 

 

 

After two cases can be seen superimposed upon the adjacent bfs sequence. But in fact either case processing is made equivalent, since v is a child node of u and u, v are adjacent in the case where in order bfs foregoing u sibling traversal has been completed (or said that all leaf nodes, because if they still have a child node, then when u traversed this layer, the first traversal of the number should not be v, u and should be the first node of this layer a child nodes), this layer and u is the last node, in fact, only this time the whole tree u can continue spreading the next layer, the next traversal sequence obtained is the last part of the whole sequence, therefore, even if v seen after the first sibling of u (v corresponding to the corresponding sub-tree is moved to the last layer, i.e. u later), because v dfs and bfs sequence corresponding subtree not change, the entire sequence will not change.

Get this conclusion, we found that as long as the judge +1 bfs (v) = bfs (u) v is greater than what u can, if it is, you can do sibling, if not, then it must be a child node.

Also learned that is read in may be pretreated in a position corresponding to each number sequence in the direct deposit like an array, look no further.

 

Code of reference code blog cited. tql

Guess you like

Origin www.cnblogs.com/jionkitten/p/12320995.html