Why Hoban-woo Was Late to School 4
InterviewTime limit1sMemory limit1024 MB
Given N king slimes and M items, report the total slime count after each item, where merging groups of sizes s and t adds s+t-1 mini slimes.
- Level
Medium4 of 10
- Topics
- Union-find, Implementation
- Solved
- No attempts yet
Problem
In fact, ever since Hoban-woo arrived in the other world, his adventure has been broadcast from Earth on the live streaming platform Twitch.
The stream currently shows Hoban-woo trying to explore a dungeon with king slimes. Every king slime belongs to a different slime group, and they are numbered from to .
Sangho, the only viewer of the stream, will spin the TWIP slot machine times and try to merge slime groups before Hoban-woo explores the dungeon.
Each spin produces an item with a pair of positive integers satisfying . The item is stored in the inventory and can be used to merge slime groups.
By consuming an item with written on it, the group containing king slime and the group containing king slime can be merged. If king slimes and already belong to the same group, nothing is merged. When two groups merge, mini slimes are created.
Sangho wants to know how many slimes (king slimes and mini slimes) can be created in the dungeon depending on the number of spins. As the streamer, let us give the answer to Sangho, the only viewer.
Input
The first line contains the number of slimes and the number of slot machine spins , separated by a space. (, )
From the second line onward, each of the next lines contains a pair of positive integers from an item obtained from the slot machine, separated by a space. ()
Output
Print lines. On line , print the maximum possible number of slimes in the dungeon when all items obtained from the first spins are used.
Hint
Since the input and output are large, fast I/O is recommended. Fast I/O methods for common languages are below.
- C++: If you use
cinandcout, you must callcin.tie(nullptr); ios::sync_with_stdio(false);once before any I/O. When starting a new line, use'\n'instead ofendl. - Java: You must use
BufferedReaderandBufferedWriter. - Python3, PyPy3: You must use
sys.stdin.readline().rstrip()instead ofinput().