UVA 10410 treereconstruction

Source: Internet
Author: User

Test instructions, give you a tree of the BFS sequence and DFS sequence, the node number of a small priority calendar, ask you a possible tree shape;

Outputs the sub-nodes of each node.

Note the following facts:

(1) The sub-tree nodes of a node in the DFS sequence must be contiguous.

(2) The subsequent node of a node u in the Bfs,dfs sequence must be either U or U's post-sibling junction {v}, or the descendant nodes {s} of U and {v}.

(3) If there is a post-sibling junction, then the BFS sequence in the back of U is bound to be the first after the brothers knot Point V1,

(4) If there are descendant nodes, then the first child node must be followed by U in the DFS sequence S1.

The BFS sequence of the Memory node U is the BFS (U) and the DFS sequence is DFS (v).

In the DFS sequence, a junction of U, node v satisfies dfs (v) = DFS (u) + 1 if BFS (v) = BFS (u) +1 and V > U; then v must be regarded as the first posterior sibling node of u,

If not, then V is the child node of U, you can launch U is the last node of the U layer in the BFS, when you do not have a post-sibling node, so the next node must be his descendants node, then v must be equivalent to the sibling node of U without changing the bfs,dfs sequence.

To this, (5) to meet the BFS (v) = BFS (u) +1 and V > U condition of V as the first post-sibling node of U, do not meet this condition must not be the post-sibling node, this can be based on the definition can be proven.

If v satisfies (5), according to (1), you and the subtree are accessed, if V does not satisfy the condition and BFS (v) >bfs (u) + 1 then v must be a sub-node of U, if BFS (v) <bfs (U) then indicates that V is its parent node, and that the subtree of U has been visited.

Iterate over the above process, the boundary condition is root. It's done!

//Rey#include <bits/stdc++.h>using namespacestd;Const intMAXN = ++5; Vector<int>G[MAXN];intPOS[MAXN];intMain () {//freopen ("In.txt", "R", stdin);   intN; intT;  while(~SCANF ("%d", &n) &&N) {       for(inti =1; I <= N; i++) scanf ("%d", &t), pos[t] =I, g[i].clear (); intRoot; scanf ("%d",&root); Stack<int>STA;      Sta.push (root);  for(inti =1; I < n; i++) {scanf ("%d",&t);  for(;;) {            intU =Sta.top (); if(pos[u]+1< Pos[t] | | (pos[u]+1= = Pos[t] && u > t) | | U = =root)               {g[u].push_back (t);               Sta.push (t);  Break; }Else{sta.pop (); }         }      }       for(inti =1; I <= N; i++) {printf ("%d:", i);  for(intj =0, Sz = G[i].size (); J < Sz; J + +) printf ("%d", G[i][j]); Puts (""); }   }   return 0;}

PS: Blogging is really good to exercise their ability to express, but also to comb the previous ideas.

UVA 10410 treereconstruction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.