Sumo
Time limit1sMemory limit128 MB
Find the earliest scheduled fight that forces two wrestlers on the same team to meet under the best two-team split.
- Level
Medium5 of 10
- Topics
- Union-find, Graph
- Solved
- No attempts yet
Problem
A Japanese monastery known for strict fasting and ascetic life runs a sumo section, and its Head decided to organise training fights for his wrestlers. He fixed the exact order of fights and the two wrestlers who meet in each one.
Moments before the first fight, the Head realised he could make the training more interesting. He could split the wrestlers into two teams so that every fight puts one team against the other. The schedule is already set and cannot be touched for whatever zen reason, and under this schedule no such split exists. That leaves one option: split the wrestlers into two teams so that the first fight between two members of the same team comes as late as possible.
Given the schedule, find the ordinal number of the first fight in which two wrestlers of the same team have to meet, assuming the teams are chosen in the best possible way. In every input such a fight does occur.
Input
The first line contains the number of wrestlers (). The wrestlers are numbered from to .
The second line contains the number of fights ().
Each of the next lines describes one fight, in the order the fights take place. Each line contains two different integers from , the numbers of the two wrestlers who meet.
Output
Print the ordinal number of the first fight between two wrestlers of the same team. This number is between and .