Judgement

시간 제한2초메모리 제한1024 MB

요약
가중치가 있는 트리에서 후보가 이웃 y로 이동할 확률이 1/w에 비례할 때, 간선 갱신 후 u에서 v까지의 기대 도달 시간을 1e9+7로 나눈 값으로 출력한다.
난이도

어려움10점 중 9점

유형
트리, 수학, 동적 계획법, 세그먼트 트리
정답자
아직 제출이 없습니다

문제

You were preparing for the KAIST RUN ICPC 2025 Contest and completely forgot to submit your calculus homework. Now the professor has declared Judgement Day.

He has unleashed the legendary Hubo robot, programmed with only one mission: find you anywhere on campus and gently encourage you to finish the assignment.

The KAIST campus can be modeled as nn buildings connected by n−1n-1 roads, forming a tree. Each road (u,v)(u, v) has a road congestion w(u,v)w(u, v).

When Hubo is at place xx, it picks its next move randomly. For each neighbor yy of xx, the probability that Hubo moves from xx to yy is Pr⁡\[x→y]=1w(x,y)∑_z∼x1w(x,z),,\Pr\[x \to y] = \frac{\tfrac{1}{w(x,y)}}{\sum\limits\_{z \sim x} \tfrac{1}{w(x,z)}} \\, , where w(x,y)w(x,y) is the congestion of road (x,y)(x,y) and z∼xz \sim x denotes all neighbors of xx.

Unfortunately for you, the professor can alter the campus roads over time, changing their loads.

You must process qq events:

  • 1 id new_w — the congestion of the road with index id becomes new_w;
  • 2 u v — you are hiding at place vv, and Hubo is deployed at place uu. Output the expected number of steps until Hubo reaches you. If u=vu=v, the answer is 00.

Since the expectation can be rational, output it modulo 109+710^9+7. Formally, if the expectation equals s/ts/t in lowest terms, print s×t−1(mod109+7)s \times t^{-1}\pmod{10^9+7}, where t−1t^{-1} is the modular inverse of tt modulo 109+710^9+7.

입력

The first line contains two integers nn and qq. Each of the next n−1n-1 lines contains three integers a,b,wa,b,w describing a road between places aa and bb with congestion ww (1≤a,b≤n1 \le a,b \le n). Roads are indexed 1,⋯ ,n−11,\cdots,n-1 in the order they appear. Each of the following qq lines is one event in the formats above.

출력

For each query of type 22, print a single integer — the expected number of steps modulo 109+710^9+7.

제한

  • 3≤n,q≤2⋅1053 \le n,q \le 2 \cdot 10^5
  • For every road congestion ww, 1≤w≤1091 \le w \le 10^9

예제1

  1. 예제 1

    입력
    4 7
    1 2 1
    2 3 1
    3 4 1
    2 1 4
    2 4 1
    1 2 2
    2 1 4
    1 1 5
    2 4 1
    2 3 3
    
    예상 출력
    9
    9
    10
    22
    0