QOJ.ac

QOJ

Süre Sınırı: 2 s Bellek Sınırı: 256 MB Toplam puan: 100

#22330. Clear Cold Water

İstatistikler

The steamy, sweltering summers of Wisconsin's dairy district stimulate the cows to slake their thirst. Farmer John pipes clear cold water into a set of $N$ $(3 \le N \le 99999; N odd)$ branching pipes conveniently numbered $1,\ldots,N$ from a pump located at the barn. As the water flows through the pipes, the summer heat warms it. Bessie wants to find the coldest water so she can enjoy the weather more than any other cow.

She has mapped the entire set of branching pipes and noted that they form a tree with its root at the farm and furthermore that every branch point has exactly two pipes exiting from it. Surprisingly, every pipe is exactly one unit in length; of course, all $N$ pipes connect up in one way or another to the pipe-tree.

Given the map of all the pipe connections, make a list of the distance from the barn for every branching point and endpoint. Bessie will use the list to find the very coldest water.

The endpoint of a pipe, which might enter a branching point or might be a spigot, is named by the pipe's number. The map contains $C$ $(1 \le C \le N)$ connections, each of which specifies three integers: the endpoint $E_{i}$ $(1 \le E_{i} \le N)$ of a pipe and two branching pipes $B1_{i}$ and $B2_{i}$ $(2 \le B1_i \le N; 2 \le B2_i \le N)$. Pipe number 1 connects to the barn; the distance from its endpoint to the barn is 1.

Input

  • Line 1: Two space-separated integers: $N$ and $C$
  • Lines $2,\ldots,C+1$: Line $i+1$ describes branching point $i$ with three space-separated integers: $E_{i}$, $B1_{i}$, and $B2_{i}$

Output

  • Lines $1,\ldots,N$: Line $i$ of the output contains a single integer that is the distance from the barn to the endpoint of pipe $i$

Examples

Input 1

5 2
3 5 4
1 2 3

Output 1

1
2
2
3
3

The input above describes this pipe map:

                    +------+
                    | Barn |
                    +------+
                       | 1
                       *
                    2 / \ 3
                         *
                      4 / \ 5

Pipe 1 is always distance 1 from the barn. Pipes 2 and 3 connect directly to pipe 1 and thus are distance 2 from the barn. Pipes 4 and 5, which connect to pipe 3, are distance 3 from the barn.

Problem credits: Rob Kolstad, 2008

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.