Water tanks the dye reaches
Statement
A farm has V water tanks, and the tanks are numbered 1 through V. Some pairs of tanks are joined directly by a pipe. Water travels both ways through a pipe, so the water in two tanks joined by a pipe mixes. There may also be a tank with no pipe on it at all.
To find out how far the water flows, the owner of the farm poured dye into tank 1. The dye spreads with the water, so it reaches every tank joined directly to tank 1 by a pipe. From a tank the dye has reached it spreads on again to the tanks joined to that one. Even tanks joined through several pipes are reached, since the dye spreads all the way along that path. A tank with no path at all leading to tank 1, on the other hand, is never reached by the dye.
Write a program that, given the number of tanks and the pipe connections, works out the number of tanks the dye reaches when dye is poured into tank 1. Tank 1, where the dye was poured, is left out of the count.
For example, suppose there are V=7 water tanks and E=6 pipes laid out as in the table below.
| Pipe number | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Tank on one side | 1 | 2 | 1 | 5 | 5 | 4 |
| Tank on the other side | 2 | 3 | 5 | 2 | 6 | 7 |
Here the dye in tank 1 spreads straight to tanks 2 and 5, and then on from tank 2 to tank 3 and from tank 5 to tank 6. Tanks 4 and 7 are joined to each other by pipe 6 and nothing more, and no path leads from them to tank 1, so the dye does not reach them.
| Tank number | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Does the dye reach it | Dye poured here | Reached | Reached | Not reached | Reached | Reached | Not reached |
The dye reaches four tanks, which are tanks 2, 3, 5 and 6. Tank 1 is left out of the count, so the answer in this example is 4.
Input and Output Examples
Example 1
Input
7 6 1 2 2 3 1 5 5 2 5 6 4 7
Output
4
Example 2
Input
5 4 1 3 5 4 5 2 1 4
Output
4
Example 3
Input
2 1 2 1
Output
1
Input
First the number of water tanks V is given on the first line. V is a natural number from 1 to 100.
Next the number of pipes E is given on the second line. E is an integer from 0 to 100.
Next the numbers of the two tanks that each pipe joins are given over E lines, separated by a space. Both numbers are natural numbers from 1 to V. A pipe joining the same two tanks is never given twice, and no pipe joins a tank to itself.
Output
Print the number of tanks the dye reaches when dye is poured into tank 1 on the first line. Tank 1, where the dye was poured, is left out of the count. If the dye reaches no tank at all, print 0.
Requirements
- Water travels both ways through a pipe, so the dye spreads in either direction
- The dye spreads all the way, even through several pipes
- Tank 1, where the dye was poured, is left out of the count
- There may be no pipes at all
- 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 2 1
1
5 4 1 3 5 4 5 2 1 4
4
7 6 1 2 2 3 1 5 5 2 5 6 4 7
4
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.