QOJ.ac

QOJ

Süre Sınırı: 2 s Bellek Sınırı: 512 MB Toplam puan: 100

#18807. 트리와 쿼리 4

İstatistikler

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

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

  • 1 i: $i$번 정점의 색을 바꾼다. (흰색 -> 검정색, 검정색 -> 흰색)
  • 2: 모든 흰색 정점 $a$와 $b$에 대해서, 가장 먼 거리를 출력한다. 이때, $a$와 $b$는 같아도 된다. 만약, 흰색 정점이 없다면 $-1$을 출력한다.

입력

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

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

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

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

간선의 거리는 항상 $-1{,}000$보다 크거나 같고, $1{,}000$보다 작거나 같은 정수이다.

출력

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

Sample

Input

3
1 2 1
1 3 1
7
2
1 1
2
1 2
2
1 3
2

Output

2
2
0
-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.