Jacob_Thomas

Queation

Sep 5th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. "Money money MONEY, I want money" thought Alex. "Now how do I get money? Well... I'll open up a camp!"
  2.  
  3. Well, unfortunately things didn't go so well for Alex's campers, and now there are N campers wandering around the city aimlessly. You have to handle Q queries; which consist of two groups finding each other and becoming one larger group. After each query, output the difference between the group of largest size and group of smallest size. If there is only one group, output 0. At first, everyone is in their own group.
  4.  
  5. Also note, if the two campers in the query are already in the same group, print the current answer, and skip merging the groups together.
  6.  
  7. Input:
  8.  
  9. The first line consists of two space separated integers, N and Q
  10.  
  11. The next Q line consists of two integers, A and B, meaning that the groups involving camper A and camper B find each other.
  12.  
  13. Output:
  14.  
  15. Output Q lines, the answer after each query.
  16.  
  17. Constraints:
  18.  
  19. 1 <= N <= 105
  20.  
  21. 1 <= Q <= 105
  22.  
  23. SAMPLE INPUT
  24. 2 1
  25. 1 2
  26. SAMPLE OUTPUT
  27. 0
  28. Explanation
  29. Here, the two campers find each other, so the answer is 0.
Add Comment
Please, Sign In to add comment