Passing On a Good Deed
Time limit1sMemory limit1024 MB
Given a directed graph and a subject K, find the start X (not K) with the most people on some path from X to K, and print X and that count.
- Level
Medium6 of 10
- Topics
- Graph, BFS, Bit manipulation
- Solved
- No attempts yet
Problem
Sangwon Lee, who has no problems with his character, often shares good deeds about the people around him. People's relationships are tangled, so when you tell someone a good deed about a person, it travels around and eventually reaches that person.
Relationships and good deeds are defined as follows.
- A relationship is one-way. That is, even if holds, may not hold.
- When a relationship is given, is one of the people close to .
- Each person may hear a good deed several times. Whenever person hears a good deed, picks one of the people close to them and passes it on.
A good deed only matters once it reaches the person it is about. Suppose a good deed starts from and reaches a third person . If it can then reach the subject through , then is called a 'direct good deed spreader' and is called an 'indirect good deed spreader'. The subject can also create indirect spreaders, and the subject can become an indirect spreader too. A direct spreader cannot become an indirect spreader.

In the relationships shown above, if the subject is person 3 and the direct spreader is person 1, then the indirect spreader is {2}, which is 1 person.
Sangwon wants to spread a good deed about the subject . He does not want to tell many people, so he will pass it to only one direct spreader. He chooses a direct spreader () and passes the good deed to them. What is the maximum number of indirect spreaders that can arise?
Input
The first line contains () and (). These are the number of people and the number of relationships, respectively.
From the second line through line , the relationships are given. Each line contains two integers and (, ) separated by a space. This means a relationship . No relationship is given twice.
The last line contains an integer (). This is the subject of the good deed.
Output
On the first line, print , the direct spreader defined in the problem, and the number of indirect spreaders caused by , separated by a space. If several values of give the same maximum, print the smallest one. If no indirect spreader can arise, print 0 on the first line.
Hint

If the good deed is passed to person 1, the spreading path is 1→2→3→1→2→4→5→6→7→8→7. The indirect spreaders are {2, 3, 4, 5, 6, 7, 8}, which is 7 people.