#T4. 分糖果
分糖果
Description
In our childhood, sharing wonderful things with friends was our joy. On this day, Child C received plenty of candies and decided to distribute them to close friends.
It is known that transferring candies from one person to another takes 1 second, and the same child will not receive candies repeatedly. Since there are enough candies, if a child receives candies at a certain moment, they will divide the candies into several portions and distribute them to those around them who have not yet received candies, while also eating some themselves. Due to their eagerness, the children can't wait to finish distributing the candies and will start eating while distributing them after receiving them.
Each child takes m seconds to finish eating the candies from the moment they receive them. So, if Child C starts distributing candies at the first second, at how many seconds will all children have finished eating their candies?
Input Format
The first line contains three numbers: n, p, c, representing the number of children, the number of connections, and the identifier of Child C.
The second line contains a number m, representing the time it takes for a child to eat the candies.
The following p lines each contain two integers, indicating that two children are next to each other.
Output Format
A single number, representing the time when all children have finished eating their candies.
4 3 1
2
1 2
2 3
1 4
5
Hint
【Sample Explanation】
At the first second, the candy is in hand 1.
At the second second, the candy is passed to hands 2 and 3.
At the third second, the candy is passed to hand 4, and hand 1 finishes eating.
At the fourth second, hands 2 and 3 finish eating.
At the fifth second, hand 4 finishes eating.
Thus, the answer is 5.
【Constraints】
- For 40% of the data: 1 ≤ n ≤ 100
- For 60% of the data: 1 ≤ n ≤ 1000
- For 100% of the data: 1 ≤ n ≤ 100000
- m ≤ n × (n - 1) / 2, and no relationship is described more than once.