QOJ.ac

QOJ

时间限制: 2 s 内存限制: 512 MB 总分: 100

#18806. 트리와 쿼리 3

统计

$N$개의 정점으로 이루어진 트리(무방향 사이클이 없는 연결 그래프)가 있다. 정점은 $1$번부터 $N$번까지 번호가 매겨져 있고, 간선은 $1$번부터 $N-1$번까지 번호가 매겨져 있다. 가장 처음에 모든 정점의 색은 흰색이다.

아래의 두 쿼리를 수행하는 프로그램을 작성하시오.

  • 1 i: i번 정점의 색을 바꾼다. (흰색 -> 검정색, 검정색 -> 흰색)
  • 2 v: 1번 정점에서 v번 정점으로 가는 경로에 존재하는 첫 번째 검정 정점의 번호를 출력한다. 만약, 그러한 정점이 없으면 -1을 출력한다.

입력

첫째 줄에 $N$ ($2 \le N \le 100{,}000$)이 주어진다.

둘째 줄부터 $N-1$개의 줄에는 i번 간선이 연결하는 두 정점 번호 u와 v가 주어진다.

다음 줄에는 쿼리의 개수 $M$ ($1 \le M \le 100{,}000$)이 주어진다.

다음 $M$개의 줄에는 쿼리가 한 줄에 하나씩 주어진다.

출력

각각의 2번 쿼리의 결과를 순서대로 한 줄에 하나씩 출력한다.

Sample

Input

9
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8
8
2 3
1 8
2 6
2 7
1 2
2 9
1 2
2 9

Output

-1
8
-1
2
-1

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.