10410 - Tree Reconstruction

来源:互联网 发布:htc刷机救砖软件 编辑:程序博客网 时间:2024/06/05 22:53

Problem H: Tree Reconstruction

You have just finished a compiler design homework question where youhad to find the parse tree of an expression. Unfortunately you left yourassignment in the library, but luckily your friend picked it up for you.Instead of e-mailing you the parse tree so that you can rewrite the solution,your friend decides to play a practical joke and sends you just the DFS and BFStrace. Rather than try to redo the entire question you decide to reconstructthe tree.

Input

The input file contains several test casesas described below.

The first line of a input is thenumber n (1 <= n <= 1000) of nodes inthe tree. The nodes in the tree are numbered 1, 2, ..., n. Theremaining numbers are the BFS traversal followed by the DFS traversal. Notethat when a parent was expanded the children were traversed in ascending order.

Output

The output for each case should consistof n lines, one for each node. Each line should start with thenode number followed by a colon followed by a list of children in ascendingorder. If there is more than one solution, any correct answer is acceptable.

Sample Input

8

4 3 5 1 2 8 7 6

4 3 1 7 2 6 5 8

Sample Output

1: 7

2: 6

3: 1 2

4: 3 5

5: 8

6:

7:

8:


Problem Setter: Jonathan Backer

b>×c�<i87` $p;array of integers defines themaze. A value of zero indicates an empty location in the maze (such as the twoempty squares in the center column of the maze in Figure 3), and a value of`-1' indicates a wild square. This input sequence is repeated for each maze inthe input. An input line containing only the word `END' (without the quotes) asthe name of the maze marks the end of the input.

Output 

The output should contain the name of eachmaze followed by its solution or the string `No Solution Possible' (without thequotes). All lines in the output file except for the maze names should beindented exactly two spaces. Maze names should start in the leftmost column.Solutions should be output as a comma-delimited sequence of the consecutivepositions traversed in the solution, starting and ending with the same square(the starting square as specified in the input). Positions should be specifiedas ordered pairs enclosed in parentheses. The solution should list 9 positionsper line (with the exception of the last line of the solution for which theremay not be a full 9 positions to list), and no spaces should be present withinor between positions.

SampleInput 

DICEMAZE1

3 3 1 2 5 1

-1 2 4

5 5 6

6 -1 -1

DICEMAZE2

4 7 2 6 3 6

6 4 6 0 2 6 4

1 2 -1 5 3 6 1

5 3 4 5 6 4 2

4 1 2 0 3 -1 6

DICEMAZE3

3 3 1 1 2 4

2 2 3

4 5 6

-1 -1 -1

END

SampleOutput 

DICEMAZE1

 (1,2),(2,2),(2,3),(3,3),(3,2),(3,1),(2,1),(1,1),(1,2)

DICEMAZE2

 (2,6),(2,5),(2,4),(2,3),(2,2),(3,2),(4,2),(4,1),(3,1),

 (2,1),(2,2),(2,3),(2,4),(2,5),(1,5),(1,6),(1,7),(2,7),

 (3,7),(4,7),(4,6),(3,6),(2,6)

DICEMAZE3

  NoSolution Possible

 


Miguel Revilla 2002-06-25

 

0 0
原创粉丝点击