Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every edge of the tree i, some number xi was written on it. In case you forget, a tree is a connected non-directed graph without cycles. After the present was granted, m guests consecutively come to Bogdan's party. When the i-th guest comes, he performs exactly one of the two possible operations:
- Chooses some number yi, and two vertecies ai and bi. After that, he moves along the edges of the tree from vertex ai to vertex biusing the shortest path (of course, such a path is unique in the tree). Every time he moves along some edge j, he replaces his current number yi by , that is, by the result of integer division yi div xj.
- Chooses some edge pi and replaces the value written in it xpi by some positive integer ci < xpi.
As Bogdan cares about his guests, he decided to ease the process. Write a program that performs all the operations requested by guests and outputs the resulting value yi for each i of the first type.
The first line of the input contains integers, n and m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ 200 000) — the number of vertecies in the tree granted to Bogdan by his mom and the number of guests that came to the party respectively.
Next n - 1 lines contain the description of the edges. The i-th of these lines contains three integers ui, vi and xi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ xi ≤ 1018), denoting an edge that connects vertecies ui and vi, with the number xi initially written on it.
The following m lines describe operations, requested by Bogdan's guests. Each description contains three or four integers and has one of the two possible forms:
- 1 ai bi yi corresponds to a guest, who chooses the operation of the first type.
- 2 pi ci corresponds to a guests, who chooses the operation of the second type.
For each guest who chooses the operation of the first type, print the result of processing the value yi through the path from ai to bi.
6 6 1 2 1 1 3 7 1 4 4 2 5 5 2 6 2 1 4 6 17 2 3 2 1 4 6 17 1 5 5 20 2 4 1 1 5 1 3
2 4 20 3
5 4 1 2 7 1 3 3 3 4 2 3 5 5 1 4 2 100 1 5 4 1 2 2 2 1 1 3 4
2 0 2
其实我觉得还应该可以用树链剖分来做,估计会更简单,有空的话实现一下。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 10;
int head[MAXN];
int cnt;
struct edge{
int v;
int next;
LL cost;
}Es[MAXN<<1];
inline void add_edge(int i,int j,LL cost){
Es[cnt].v = j;
Es[cnt].cost = cost;
Es[cnt].next = head[i];
head[i] = cnt++;
}
LL dep[MAXN],fa[MAXN],dist[MAXN];
void dfs(int u,int _dep){
dep[u] = _dep;
for(int e = head[u];e != -1;e = Es[e].next){
int v = Es[e].v;
if(v != fa[u]){
LL cost = Es[e].cost;
dist[v] = cost;
fa[v] = u;
dfs(v,_dep+1);
}
}
}
int es_u[MAXN],es_v[MAXN];LL es_w[MAXN];
int parent[MAXN];
int n,m;
int find(int x){
return x == parent[x] ? x : parent[x] = find(parent[x]);
}
void init(){
cnt = 0;
memset(head,-1,sizeof(head));
for(int i = 0;i < MAXN;i++){
parent[i] = i;
}
}
void query(int u,int v,LL w){
while(u != v && w){
if(dep[u] < dep[v]) swap(u,v);
if(dist[u] == 1){
u = find(u);
}
else{
w /= dist[u];
u = fa[u];
}
}
printf("%lld\n",w);
}
int main(){
init();
scanf("%d%d",&n,&m);
for(int i = 1;i <= n-1 ;i++){
int u,v;LL w ;
scanf("%d%d%lld",&u,&v,&w);
es_u[i] = u,es_v[i] = v,es_w[i] = w;
add_edge(u,v,w);
add_edge(v,u,w);
}
dfs(1,0);
for(int i = 1;i <= n-1;i++){
if(es_w[i] != 1) continue;
int u = es_u[i],v = es_v[i];
if(dep[u] < dep[v]) swap(u,v);
parent[u] = find(v);
}
for(int i = 0;i < m;i++){
int op;scanf("%d",&op);
if(op == 1){
int u,v;LL w;
scanf("%d%d%lld",&u,&v,&w);
query(u,v,w);
}
else{
int id;LL w;
scanf("%d%lld",&id,&w);
int u = dep[es_u[id]] > dep[es_v[id]] ? es_u[id] : es_v[id];
dist[u] = w;
if(w == 1){
parent[u] = find(es_u[id] + es_v[id] - u);
}
}
}
return 0;
}