Members who hear the news on day K
Statement
A club has N members, numbered from 1 to N. There are M relations that pass news between the members, and each relation works in one direction only. If there is a relation from member A to member B, then A passes news to B, but B does not pass news to A through that relation. News travels both ways only when a relation in the opposite direction is given separately.
The news spreads one step per day. On the day after a member hears the news, every member that member passes news to hears it. If a member would hear the news along several paths, only the first day that member hears it counts. A member who is not linked by any path never hears the news at all.
Suppose member X first brought the news up on day 0. Write a program that finds every member whose first day of hearing the news is exactly day K.
For example, suppose there are N=5 members and M=5 relations that pass news, as in the table below.
| Relation | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Member who passes it | 1 | 3 | 3 | 1 | 5 |
| Member who hears it | 4 | 5 | 2 | 3 | 4 |
If member X=3 brought the news up, the first day each member hears it is as follows. A member who never hears the news is left blank.
| Member | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| First day of hearing it | Day 1 | Day 0 | Day 2 | Day 1 |
Member 3 is the one who brought the news up, so that is day 0. Members 5 and 2, whom member 3 passes news to, hear it on day 1. Member 4, whom member 5 passes news to, hears it on day 2. No relation passes news to member 1, so member 1 never hears it.
The members whose first day of hearing the news is day K=1 are members 2 and 5, and the numbers of the two members are printed in ascending order.
Input and Output Examples
Example 1
Input
5 5 1 3 1 4 3 5 3 2 1 3 5 4
Output
2 5
Example 2
Input
2 1 1 1 2 1
Output
-1
Example 3
Input
4 4 2 1 4 2 1 3 3 4 4 3
Output
4
Input
First the number of members N, the number of relations M that pass news, the day K to look at, and the number X of the member who first brought the news up are given in order on one line, separated by spaces. N is a natural number from 2 to 300,000, and M is a natural number from 1 to 1,000,000. K is a natural number from 1 to 300,000, and X is a natural number from 1 to N.
Then M lines follow, each giving one relation. Each line holds two numbers A and B separated by a space, which means that member A passes news to member B. A and B are both natural numbers from 1 to N and differ from each other. The same relation in the same direction is never given twice.
Output
Print the numbers of the members whose first day of hearing the news is exactly day K in ascending order, one per line. If there is no such member, print -1 on one line.
Requirements
- A relation that passes news works in one direction only
- The news spreads one step per day
- Member X, who first brought the news up, counts as day 0
- Pick only the members whose first day of hearing it is exactly day K
- Print the numbers in ascending order, one per line, and print -1 if there are none
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 1 1 1 2 1
-1
4 4 2 1 4 2 1 3 3 4 4 3
4
5 5 1 3 1 4 3 5 3 2 1 3 5 4
2 5
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.