#P4299. 首都

    ID: 3227 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>搜索并查集剪枝Link-Cut Tree,LCT

首都

Description

On Planet X there are nn countries, each occupying exactly one city of Planet X. Cities are numbered from 11 to nn. 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 xx and yy 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 xx.
  • 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 nn and the number of operations mm.

Then follow mm lines. Each line starts with a string opop 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 1n1051 \leq n \leq 10^5, 1m2×1051 \leq m \leq 2 \times 10^5, and 1x,yn1 \leq x, y \leq n.

Translated by ChatGPT 5