Grapevine | 프로그래밍의 벗 PivotOJ
PivotOJ

Grapevine

시간 제한: 3000ms메모리 제한: 1024MB출처: NOI 2022BOJ 27291

문제

Syrup the Turtle waters the huge Grapevine which snakes around town. The layout of the Grapevine can be best described as having NN leafy joints, which Syrup has numbered 11 to NN, connected by N1N - 1 branches also numbered 11 to N1N - 1. Each branch ii directly connects two joints AiA_i and BiB_i, and has a length of WiW_i. No two branches directly connect the same pair of joints, and as part of the single Grapevine, every joint is connected to every other either directly or indirectly by branches.

With his sturdy hose and a little handiwork, Syrup is able to sway the growth of the Grapevine as he deems fit. In particular, he can soak a joint of the vine - causing it to immediately sprout a single giant grape if it was empty, or instead dislodging the grape there if one was present.

He can also anneal a branch with water, extending or shortening it to a new length wiw_i by spraying at the right pressure and angle. To make sure things are on track, Syrup will periodically stand atop an elevated joint and seek for the closest grape. The distance from such a joint to a grape is defined by the shortest path starting from said joint, traversing a number of branches (or none at all), and ending at the grape’s own joint.

Right now, the Grapevine has no grapes attached after the last passing storm. Syrup has his watering agenda of QQ actions planned out for the week and is about to begin spraying; but first, he needs to know what distances to expect when he seeks for grapes along the way. Given Syrup’s watering plans, your task is to find for each planned seek the distance from the specified joint to the nearest grape.

입력

Your program must read from standard input.

The first line contains two integers, NN and QQ.

N1N - 1 lines will follow. The iith line contains three integers, AiA_i, BiB_i, and WiW_i, describing a single branch.

QQ lines will follow, each representing an action by Syrup.

  • If the first integer of the line is 11, it represents a seek and 11 integer qiq_i will follow. This means that you are to determine the distance between joint qiq_i and the nearest joint with a grape, and output this distance. If there are no grapes on the Grapevine, output 1-1 instead.
  • If the first integer of the line is 22, it represents a soak and 11 integer uiu_i will follow. This means that joint uiu_i is soaked and grows a grape or has its grape dislodged.
  • If the first integer of the line is 33, it represents an anneal and 33 integers aia_i, bib_i, and wiw_i will follow. This means that the length of the branch between joints aia_i and bib_i has had its length changed to wiw_i. It is guaranteed that a branch between joints aia_i and bib_i exists.

출력

Your program must print to standard output.

For each seek action, output one line with a single integer, the distance to the closest grape, or 1-1 if no grapes are present.

예제

예제 1

입력
7 11
1 2 2
2 3 4
5 6 1
5 3 6
3 7 6
2 4 9
2 6
2 4
2 7
1 1
3 2 3 0
1 1
3 6 5 0
1 1
3 3 5 0
3 2 4 0
1 1
출력
11
8
8
2

예제 2

입력
6 11
1 2 3
1 3 4
2 4 1
2 5 4
3 6 6
2 3
1 2
2 4
3 1 3 2
1 1
2 3
3 2 1 2
2 4
1 3
2 2
1 3
출력
7
2
-1
4

예제 3

입력
7 8
1 2 2
2 3 3
3 6 2
4 6 1
5 6 4
6 7 3
2 3
1 4
2 3
2 5
1 1
3 6 7 4
1 5
1 7
출력
3
11
0
8
코드를 제출하려면 로그인하세요.