#P4299. 首都
首都
Description
On Planet X there are countries, each occupying exactly one city of Planet X. Cities are numbered from to . Since countries are hostile to each other, there are no roads between cities of different countries.
Wars often break out on Planet X. If country A defeats country B, then B disappears from the planet forever, and B’s territory comes under A’s control. To strengthen governance, the king of A will build a road between A and B: specifically, he chooses one original city of A and one city of B and builds a road connecting these two cities.
To facilitate governing the country, the capital is chosen as the city that minimizes the sum of distances from all other cities in that country; here, distance means the number of roads traversed. If there are multiple such cities, the one with the smallest index becomes the capital.
You are given the wars that happen on Planet X and need to process information about capitals. Specifically, there are three types of operations to handle:
A x y: Two countries go to war. The winner chooses cities and and builds a road between them (it is guaranteed that one of the two cities belongs to the winner and the other to the loser).Q x: Query the current capital of the country containing city .Xor: Query the bitwise XOR of the indices of all current countries’ capitals.
Input Format
The first line contains two integers, the number of cities and the number of operations .
Then follow lines. Each line starts with a string indicating the operation type, followed by several integers, with formats as described in the Description.
Output Format
For each Q operation and Xor operation, output one line with a single integer as the answer.
10 10
Xor
Q 1
A 10 1
A 1 4
Q 4
Q 10
A 7 6
Xor
Q 7
Xor
11
1
1
1
2
6
2
Hint
Constraints
For all testdata, it is guaranteed that , , and .
Translated by ChatGPT 5
京公网安备 11011102002149号