#C11426. Minimum Time to Infect All People
Minimum Time to Infect All People
Minimum Time to Infect All People
In a village, there are n people numbered from 1 to n. The social connections in the village are represented as m bidirectional edges. At time 0, only person 1 is infected. Every time unit, each infected person simultaneously infects all of their direct neighbors that are not yet infected.
Your task is to determine the minimum number of time units required to infect all people in the village. If it is impossible to infect everyone, output -1
.
Mathematically, if we define the infection time of a person v as the minimum number of steps needed to reach v from person 1, then the answer is given by
\[
T = \max_{v \in \{1,2,\dots,n\}} (\text{time}(v))
\]
if every person is reachable; otherwise, the answer is -1
.
inputFormat
The input is read from stdin and has the following format:
n m u1 v1 u2 v2 ... um vm
Here, the first line contains two integers n
and m
, representing the number of people and the number of connections, respectively. Each of the following m
lines contains two integers u
and v
indicating that there is a bidirectional connection between person u
and person v
.
outputFormat
Output to stdout a single integer representing the minimum number of time units required to infect all the people. If it is impossible, output -1
.
4 3
1 2
2 3
3 4
3
</p>