Under construction

Still being built. Things may change

Hyacinth frequency assignment

Time limit1sMemory limit256 MB

Summary
Assign one frequency to each edge of a tree by the stated DFS rule so each node uses at most two frequencies.
Level

Easy3 of 10

Topics
Tree, DFS, Simulation
Solved
No attempts yet

Problem

In the multi channel mesh network architecture called Hyacinth, every node of a wireless mesh network carries exactly two network interface cards (NICs). You choose one frequency for each NIC. Two nodes that are in range of each other can communicate only if their NICs share at least one frequency, so a single node works with at most two frequencies.

A frequency is used when two nodes that are in range of each other both carry that frequency. The more frequencies the network uses, the better its theoretical throughput. Assign frequencies to the 2n2n NICs so that every pair of nodes in range of each other can communicate, and make the number of used frequencies as large as possible. The links of the network form a tree.

Input

The first line contains one integer nn (2≤n≤10 0002 \le n \le 10\,000), the number of nodes in the network.

Each of the next n−1n - 1 lines contains two integers ii and jj (1≤i,j≤n1 \le i, j \le n), meaning that node ii and node jj are in range of each other. Nodes are numbered from 1 to nn, and the given links form a tree.

Output

Print nn lines. Line ii contains the two frequencies assigned to the two NICs of node ii. A frequency is a nonnegative integer smaller than 10910^9.

Many assignments reach the maximum, so print the one produced by this rule.

  • Sort every adjacency list by node number and root the tree at node 1.
  • Frequencies are taken in the order 1, 2, 3, and so on. The value 0 marks a NIC that no link uses.
  • If node 1 has exactly one neighbour, give that link frequency 1. If node 1 has two or more neighbours, give the link to its smallest numbered neighbour frequency 1, and give every other link of node 1 frequency 2.
  • Run a depth first search from node 1 that always continues into the smallest numbered unvisited neighbour. When the search first reaches a node vv other than node 1 and vv has at least one child, take the smallest frequency not taken yet and put it on every link from vv to its children.
  • On line ii, print the two frequencies of node ii in increasing order. If node ii takes part in only one frequency, the first value on the line is 0.

Examples2

  1. Example 1

    Input
    2
    1 2
    
    Expected output
    0 1
    0 1
    
  2. Example 2

    Input
    14
    1 2
    1 3
    1 4
    2 5
    2 6
    3 7
    4 8
    4 9
    4 10
    7 11
    7 12
    7 13
    7 14
    
    Expected output
    1 2
    1 3
    2 4
    2 6
    0 3
    0 3
    4 5
    0 6
    0 6
    0 6
    0 5
    0 5
    0 5
    0 5