P27768 [洛谷] Drawing a Binary Tree
提高-
题目描述

Problem Description

A binary tree is a fundamental data structure that is either empty or consists of a root node, a left subtree, and a right subtree, where both the left and right subtrees are also binary trees.

When a binary tree has a height of $m-1$, it has $m$ levels. If a binary tree has all levels fully populated except for the $m^{th}$ level, and all leaf nodes are on the $m^{th}$ level, it is considered a full binary tree.

You are required to write a program to draw a binary tree, which is formed by removing several nodes from a full binary tree. For a full binary tree, we need to draw it according to the following rules:

  1. Nodes are represented by the lowercase letter o. For a parent node, the left subtree is connected by /, and the right subtree is connected by \.

  2. Define $[i,j]$ as the character located at the $i^{th}$ row and $j^{th}$ column. If $[i,j]$ is /, then $[i-1,j+1]$ and $[i+1,j-1]$ are either o or /. If $[i,j]$ is \, then $[i-1,j-1]$ and $[i+1,j+1]$ are either o or \. Similarly, if $[i,j]$ is a node o on levels $1$ to $m-1$, then $[i+1,j-1]$ is / and $[i+1,j+1]$ is \.

  3. For nodes on the $m^{th}$ level, which are leaf nodes, if two nodes belong to the same parent, they are separated by $3$ spaces; if two nodes are adjacent but do not belong to the same parent, they are separated by $1$ space. The first node on the $m^{th}$ level does not have a preceding space.

Finally, you need to remove $n$ nodes (including their left and right subtrees and the connection to their parent) from a fully drawn binary tree, replacing the original characters with spaces (space is ASCII 32, and outputting ASCII 0 will be considered an incorrect answer).

Input Format

The first line contains two positive integers $m$ and $n$, representing the number of levels in the binary tree and the number of nodes to be removed, respectively.

The next $n$ lines, each containing two positive integers, indicate the removal of the $j^{th}$ node on the $i^{th}$ level.

Output Format

The binary tree drawn according to the problem requirements.

Sample Input and Output

Sample Input #1

2 0

Sample Output #1

  o  
 / \ 
o   o

Sample Input #2

4 0

Sample Output #2

           o           
          / \          
         /   \         
        /     \        
       /       \       
      /         \      
     o           o     
    / \         / \    
   /   \       /   \   
  o     o     o     o  
 / \   / \   / \   / \ 
o   o o   o o   o o   o

Sample Input #3

4 3
3 2
4 1
3 4

Sample Output #3

           o           
          / \          
         /   \         
        /     \        
       /       \       
      /         \      
     o           o     
    /           /      
   /           /       
  o           o        
   \         / \       
    o       o   o      

Notes/Hints

$30\%$ of the test cases satisfy: $n=0$;

$50\%$ of the test cases satisfy: $2 \le m \le 5$;

$100\%$ of the test cases satisfy: $2 \le m \le 10, 0 \le n \le 10, 1 < i \le m, j \le 2^{i-1}$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 0
样例输出
o / \ o o