eolymp
bolt
Try our new interface for solving problems
Problems

Emails

Emails

Ariadna's blog is filled with delicious recipes and sensible advice for a healthy and balanced lifestyle. Unsurprisingly, it has thus gathered an impressive number of readers. This reader base is now stable, and Ariadna feels that it would be useful for them to interact more and form a tighter community, one that is not solely anchored to the blog. Ariadna knows that some of the readers are already friends or acquaintances, and therefore have each other's email addresses. She thinks that a good start for developing the community would be for everyone to have everyone else's email address, so that everyone would be able to reach out to the entire group. Since she knows her blog's readers also greatly enjoy doing things in a "decentralized" fashion, she therefore devises the following protocol, to be started on day $d$: \begin{itemize} \item Every day at $8$ am, everyone sends the current list of contacts in their address book to all of the contacts in their address book. \item Every day at $8$ pm, everyone updates their address book, adding any new received email addresses. \end{itemize} If a person does not need to do any update at $8$ pm, then the process is said to have converged for this person, and she will no longer need to continue sending emails over the next days. You are a skillful hacker and you have managed to get access to all of the blog readers' address books. You would like to surprise and impress Ariadna by notifying her of whether or not the process she proposes will lead to everyone getting everyone else's address. Moreover, if the process is meant to succeed, you want to give her a good estimate of how many days it would take. More precisely, if the process succeeds, you can either give her: \begin{itemize} \item the number $e$ of days (including the first day) elapsed until the last update takes place, or \item the number of days (including the first day) elapsed until the process has converged on everyone's side. Note that, according to Ariadna's definition, this is equal to $e + 1$. \end{itemize} \InputFile The first line contains two integers $n~(2 \le n \le 10^5)$ and $m~(1 \le m \le 10^5)$, corresponding to the number of readers and respectively to the number of pairs of readers that initially have each other's email address. Readers are numbered from $1$ to $n$. The $m$ following lines each contain two integers, $i$ and $j$, meaning that readers $i$ and $j$ initially have each other's email address. Note that this means that both reader $i$ has reader $j$'s address and reader $j$ has reader $i$'s address. \OutputFile The output should contain a single integer equal to either: \begin{itemize} \item $-1$ if the process does not lead to everyone eventually having everyone else's email address, or \item the estimated necessary number of days, otherwise. Note that this number may be equal to $0$. \end{itemize} \Note We assume the reader base is stable, i.e. no reader leaves and no additional reader joins throughout the process. We assume that everyone knows their own email address; receiving one’s own address is simply ignored. You do no have to be "consistent" in your answers across several tests cases, meaning that you can output the value $e$ for one test case and $e + 1$ for another. \Examples Sample Explanation $1$. The process proceeds as follows: On day $d$ at $8$ am: \begin{itemize} \item Reader $1$ sends the address of reader $2$ to reader $2$. \item Reader $2$ sends the addresses of readers $1$ and $3$ to readers $1$ and $3$. \item Reader $3$ sends the addresses of readers $2$ and $4$ to readers $2$ and $4$. \item Reader $4$ sends the address of reader $3$ to reader $3$. \end{itemize} On day $d$ after the $8$ pm update: \begin{itemize} \item Reader 1's address-book has been updated and contains the addresses of readers $2$ and $3$. \item Reader 2's address-book has been updated and contains the addresses of readers $1, 3$ and $4$. \item Reader 3's address-book has been updated and contains the addresses of readers $1, 2$ and $4$. \item Reader 4's address-book has been updated and contains the addresses of readers $2$ and $3$. \end{itemize} On day $d + 1$ at $8$ am: \begin{itemize} \item Reader $1$ sends the addresses of readers $2$ and $3$ to readers $2$ and $3$. \item Reader $2$ sends the addresses of readers $1, 3$ and $4$ to readers $1, 3$ and $4$. \item Reader $3$ sends the addresses of readers $1, 2$ and $4$ to readers $1, 2$ and $4$. \item Reader $4$ sends the addresses of readers $2$ and $3$ to readers $2$ and $3$. \end{itemize} On day $d + 1$ after the $8$ pm update: \begin{itemize} \item Reader $1$'s address-book has been updated and contains the addresses of readers $2, 3$ and $4$. \item The process has converged for reader $2$ since there is no update. \item The process has converged for reader $3$ since there is no update. \item Reader $4$'s address-book has been updated and contains the addresses of readers $1, 2$ and $3$. \end{itemize} On day $d + 2$ at $8$ am: \begin{itemize} \item Reader $1$ sends the addresses of readers $2, 3$ and $4$ to readers $2, 3$ and $4$. \item Reader $4$ sends the addresses of readers $1, 2$ and $3$ to readers $1, 2$ and $3$. \end{itemize} On day $d + 2$ after the $8$ pm update: \begin{itemize} \item The process has converged for reader $1$ since there is no update. \item The process has converged for reader $4$ since there is no update. \end{itemize} The last update takes place on day $d + 1$, after $2$ elapsed days. The process has converged for everyone on day $d + 2$, after $3$ elapsed days. The sample output contains the former value $2$. Outputting the latter value $3$ is an equally correct alternative.
Time limit 1 second
Memory limit 128 MiB
Input example #1
4 3
1 2
2 3
3 4
Output example #1
2
Input example #2
6 3
1 2
3 4
5 6
Output example #2
-1
Source 2020 ACM Southwestern Europe Regional Contest (SWERC), Paris, March 7 (2021), Problem I