## DMOPC '14 Contest 5 P6 - Save Nagato!

View as PDF

Points: 20 (partial)
Time limit: 3.0s
Memory limit: 256M

Author:
Problem types

You are about to change history!

You have recently been informed of cruel and inhumane tests on Nagato by the classless Am*ricans. Being a responsible citizen, you have decided to take matters into your own hands and stop the imbecile Am*ricans from destroying such a beautiful and majestic ship.

The Un*ted St*tes Armed Forces (made up of those Am*ricans, henceforth known as USAF) has members and a hierarchy in which every member (except the President) has exactly one direct superior. Each member of the USAF has a rank. The rank of the President is 1, and each other member's rank is the rank of their direct superior plus 1. For example, if someone's direct superior was the President, their rank would be 2. It is guaranteed that if you follow the chain of superiors of any member of the USAF, you will eventually reach the President.

Your master plan is to immerse the President in anime and manga until he realizes the foolishness of his actions and becomes a weeaboo, after which he will immediately cancel the planned destruction of the Nagato.

You have already gathered a lot of information of the USAF, but you cannot execute your plan yet as some crucial pieces of information are missing. Although you know information such as "these two people form an inferior-superior pair" (that is, one of them is the direct superior of the other), you do not know who is the superior and you do not actually know which person in the USAF (numbered from to ) is the President. And most alarmingly, you do not know how many unique ranks there are among members of the USAF (every rank has a specific etiquette guideline you must learn before you can infiltrate the USAF).

Since the President's number is highly classified, you would like to determine the number of unique ranks in the USAF regardless of which of the members to you assume is the President.

#### Input Specification

The first line of input will have .
The next lines of input will each contain a superior-inferior pair . It is guaranteed that the information corresponds to a valid hierarchy assuming each of the members is the President.

#### Output Specification

You should output lines. On the line, you should output the number of unique ranks assuming that member is the President.

#### Sample Input

3
2 1
2 3

#### Sample Output

3
2
3

#### Explanation for Sample Input

The following diagrams shows the hierarchy of the USAF for each of the three cases where person 1, 2, and 3 are the President. The star next to each person shows their rank.

##### Case 1

There are 3 unique ranks: 1, 2, and 3.

##### Case 2

There are 2 unique ranks: 1 and 2. Note that both member 1 and member 2 have rank 2, so it's only counted once.

##### Case 3

There are 3 unique ranks: 1, 2, and 3. Note that this case is symmetrical to the first case.

Everything appearing in this problem is fictitious. Any resemblance to real countries, people, or battleships is purely coincidental. Nagato

• Riolku  commented on Dec. 20, 2018, 5:06 p.m. edited

Despite rewriting my solution multiple times and using fast input, I am still unable to pass any cases in the last batch. My solution is as illustrated by the editorial.

EDIT: I found the reason.

• hahahajokes  commented on Nov. 29, 2017, 8:16 p.m.

why do I have either RET or TLE after question 5 at batch 3 using java 7-10?

• felixzhang25  commented on Aug. 23, 2018, 9:14 a.m. edited

RET?!?!? THATS RTE!!!!!!! RunTimeError!!!!