#P17163. [CEOI 2026] Birdwatchers
[CEOI 2026] Birdwatchers
Problem Description
The Birdwatchers' Society of San Serriffe has a curiously bloated and ever-changing internal structure. The Society consists of chapters, and each member of the Society belongs to exactly one chapter. The chapters are numbered from to and the -th chapter has members. Thus the Society has a total of members.
Each chapter is led by one of its members, who in this role is called the officer of the chapter. The officers are numbered the same as the chapters, so that (for each ) officer is the one in charge of chapter .
Moreover, the officers are organized hierarchically through a system of mentorship: each officer except one has a mentor, who is the officer of some other chapter. The only officer without a mentor is the President of the Society. If officer is the mentor of officer , we also say that officer is a disciple of officer . No officer is, directly or indirectly, a mentor of themselves; thus, by following the sequence from an officer to their mentor, their mentor's mentor, and so on, we eventually always reach the President.
We define the influence of an officer as the sum of the number of members in his chapter and of the influences of all his disciples (if he has any). It will be easily seen that the officer with the highest influence is the President, whose influence always equals . An officer is called a senior officer if his influence is .
The Bylaws of the Society specify that the senior officer with the lowest influence (amongst all the senior officers) shall act as the Treasurer of the Society.
From time to time, an officer (other than the President) may change his allegiance, so that he is thenceforth the disciple of a different mentor than before (provided that his new mentor is not one of his disciples, or his disciples' disciples, etc.). Because of this, it can happen that the influence of some officers changes and the role of Treasurer falls to a different officer than before.
Task
Write a program that reads the description of the initial state of the Society and a sequence of changes of allegiance. Your program must print who the Treasurer is in the initial state of the Society as well as after each change of allegiance.
Input Format
The first line contains two integers, and , separated by a space; is the number of chapters, is the number of changes of allegiance.
The next lines describe the initial state of the Society. The -th of these lines contains two integers, and , separated by a space; is the mentor of officer (i.e. of the officer in charge of chapter ), while is the number of members of chapter . The value indicates that officer is the President of the Society and thus has no mentor.
The remaining lines describe the changes of allegiance. The -th of these lines contains two integers, and , separated by a space. The meaning of these integers is as follows. Let us denote by (for ) the Treasurer after the first changes of allegiance (thus is the initial Treasurer before the first change of allegiance). Then the -th change of allegiance consists of officer becoming the new mentor of officer , where and . The purpose of this representation of the values and is to force your program to process the changes of allegiance in the order in which they appear.
The changes of allegiance in the input data will always be valid, i.e. will not be equal to , nor will be a disciple of , a disciple of a disciple of , etc. It is, however, possible that was already the mentor of just before the -th change (so that nothing actually changes at that point).
Note that if your program calculates the wrong result at some point, it will decode the subsequent inputs , , etc., incorrectly as well, and might crash with an RTE (runtime error) verdict instead of a WA (wrong answer) verdict because the incorrectly decoded inputs might be invalid (e.g. it might erroneously obtain a which is a disciple of ).
Output Format
Print the numbers , each on its own line, where is the Treasurer after the first changes of allegiance. Naturally, each must be an integer from the range .
7 2
0 1
1 3
1 3
2 3
2 1
5 2
5 1
3 7
2 7
2
2
3
Hint
Comment
Initially, officer is the Treasurer (hence ). In the first change of allegiance, we read and and calculate and ; thus, officer becomes the new mentor of officer ; officer remains Treasurer (hence ). In the second change of allegiance, we read and and calculate and ; thus, officer becomes the new mentor of officer and also becomes the new Treasurer (hence ).
Constraints
- for each
- and for each .
Subtasks
- Subtask ( points):
- Subtask ( points):
- Subtask ( points):
- Subtask ( points): No additional constraints.