Finding teammates - MarisaOJ: Marisa Online Judge

Finding teammates

Time limit: 1000 ms
Memory limit: 512 MB
The students of the magic class led by Marisa are about to play a game. The class consists of $n$ students, and Marisa will try to divide the class into $2$ teams so that each student belongs to exactly one team. Marisa has invited two coaches, Reimu and Reisen, to lead the two teams. Marisa has a list of $m$ pieces of information, where the $i$-th piece includes two integers $u_i, v_i$, indicating that students $u_i$ and $v_i$ have practiced together before.To ensure fairness, Marisa wants the two teams led by the coaches to satisfy the following conditions: - All students within the same team must have practiced with each other before. This ensures that group activities will be more effective. - The difference in the number of students between the two teams is minimized. Help Marisa find the smallest possible difference in the number of students between the two teams. If it is not possible to divide the class in such a way, print $-1$. ### Input - The first line contains two integers $n, m$. - The next $m$ lines, each containing two positive integers $u_i, v_i$, indicate that students $u_i$ and $v_i$ have practiced together. ### Output - Print the answer to the problem. ### Constraints - $2 \le n \le 1000.$ - $0 \le m \le \dfrac{n(n-1)}{2}.$ ### Example Input ``` 5 7 1 2 1 3 3 4 5 4 2 5 4 1 2 3 ``` Output ``` 1 ``` Input ``` 5 6 1 2 3 4 4 3 1 5 5 4 2 3 ``` Output ``` -1 ``` Input ``` 7 14 1 2 2 3 3 1 3 4 3 5 4 5 5 7 7 6 6 4 4 7 5 6 1 4 1 7 7 1 ``` Output ``` 1 ```