Hyacinth frequency assignment
Time limit1sMemory limit256 MB
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 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 (), the number of nodes in the network.
Each of the next lines contains two integers and (), meaning that node and node are in range of each other. Nodes are numbered from 1 to , and the given links form a tree.
Output
Print lines. Line contains the two frequencies assigned to the two NICs of node . A frequency is a nonnegative integer smaller than .
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 other than node 1 and has at least one child, take the smallest frequency not taken yet and put it on every link from to its children.
- On line , print the two frequencies of node in increasing order. If node takes part in only one frequency, the first value on the line is 0.