QOJ.ac

QOJ

時間限制: 5 s 記憶體限制: 256 MB 總分: 100

#13827. Hide and Seek

统计

Jiajia and Wind are a loving couple with many children. One day, Jiajia, Wind, and their children decide to play hide-and-seek at home. Their house is large and has a peculiar structure, consisting of $N$ rooms and $N-1$ bidirectional corridors, such that any two rooms are reachable from each other.

The game proceeds as follows: the children are responsible for hiding, Jiajia is responsible for searching, and Wind is responsible for operating the lights in these $N$ rooms. Initially, all lights are off. Each time, the children only hide in rooms where the light is off. To make it more exciting, the children will ask to turn the light on or off in a specific room. To evaluate the complexity of a game, Jiajia wants to know the maximum distance between any two children (i.e., the distance between the two farthest rooms with lights off).

We define each operation as follows:

Operation Description
C(hange) i Toggle the lighting state of room $i$. If it was on, turn it off; if it was off, turn it on.
G(ame) Start a game and query the distance between the two farthest rooms with lights off.

Input

The first line contains an integer $N$, representing the number of rooms, which are numbered $1, 2, 3, \dots, N$. The next $N-1$ lines each contain two integers $a$ and $b$, indicating a corridor between room $a$ and room $b$. The next line contains an integer $Q$, representing the number of operations. The following $Q$ lines each contain an operation as described above.

Output

For each Game operation, output a non-negative integer representing the distance between the two farthest rooms with lights off. If only one room has its light off, output $0$. If all rooms have their lights on, output $-1$.

Examples

Input 1

8
1 2
2 3
3 4
3 5
3 6
6 7
6 8
7
G
C 1
G
C 2
G
C 1
G

Output 1

4
3
3
4

Constraints

For 20% of the data: $N \le 50, Q \le 100$; For 60% of the data: $N \le 3000, Q \le 10000$; For 100% of the data: $N \le 100000, Q \le 500000$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.