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
树上的路径,少不了lca,然后我们发现答案减少很快,所以我们想办法除去权为1的边就好了,所以这里可以用并查集来维护
/*======================================================
# Author: whai
# Last modified: 2015-11-05 01:36
# Filename: d.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <tr1/unordered_map>
using namespace std;
using namespace std::tr1;
#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second
const int N = 2 * 1e5 + 5;
const int LOGN = 22;
vector<int> tree[N];
int fa[N][LOGN];
int depth[N];
//map<P, LL> C;
unordered_map<LL, LL> C;
LL get_p(int x, int y) {
return (LL)x * N + y;
}
void dfs(int u, int p, int d) {
depth[u] = d;
fa[u][0] = p;
for (int i = 0; i < tree[u].size(); ++i) {
if (tree[u][i] != p)
dfs(tree[u][i], u, d + 1);
}
}
int LCA(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
for (int i = 0; i < LOGN; ++i) {
if (((depth[v] - depth[u]) >> i) & 1)
v = fa[v][i];
}
if (u == v) return u;
for (int i = LOGN - 1; i >= 0; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
int f[N];
int get_fa(int x) {
if(x != f[x]) return f[x] = get_fa(f[x]);
return x;
}
void predo(int n) {
int root = 1;
dfs(root, 0, 0);
depth[0] = -1;
for (int j = 0; j + 1 < LOGN; ++j) {
for (int i = 1; i <= n; ++i) {
if (fa[i][j] < 0) fa[i][j + 1] = -1;
else fa[i][j + 1] = fa[fa[i][j]][j];
}
}
for(int i = 0; i <= n; ++i) {
f[i] = i;
}
}
void init(int n) {
for (int i = 0; i <= n; ++i)
tree[i].clear();
}
LL s[N], tot;
void Q1(int a, int b, LL y) {
int ac = LCA(a, b);
int now = a;
LL ans = y;
while(depth[now] > depth[ac]) {
int nxt = fa[now][0];
int u = now, v = nxt;
if(u > v) swap(u, v);
LL c = C[get_p(u, v)];
if(c == 1) {
int f_now = get_fa(now);
int f_nxt = get_fa(nxt);
f[f_now] = f_nxt;
now = f_nxt;
} else {
ans /= c;
now = get_fa(nxt);
}
if(ans == 0) break;
}
if(ans == 0) {
puts("0");
return ;
}
tot = 0;
now = b;
LL tmp = ans;
while(depth[now] > depth[ac]) {
int nxt = fa[now][0];
int u = now, v = nxt;
if(u > v) swap(u, v);
LL c = C[get_p(u, v)];
if(c == 1) {
int f_now = get_fa(now);
int f_nxt = get_fa(nxt);
f[f_now] = f_nxt;
now = f_nxt;
} else {
tmp /= c;
s[tot++] = c;
now = get_fa(nxt);
}
if(tmp == 0) break;
}
if(tmp == 0) {
puts("0");
return ;
}
for(int i = tot - 1; i >= 0; --i) {
ans /= s[i];
}
printf("%I64d\n", ans);
}
int u[N], v[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
init(n);
LL c;
for (int i = 1; i < n; ++i) {
scanf("%d%d%I64d", &u[i], &v[i], &c);
if(u[i] > v[i]) swap(u[i], v[i]);
tree[u[i]].PB(v[i]);
tree[v[i]].PB(u[i]);
C[get_p(u[i], v[i])] = c;
}
predo(n);
for(int i = 0; i < m; ++i) {
int op;
scanf("%d", &op);
if(op == 1) {
int a, b;
LL y;
scanf("%d%d%I64d", &a, &b, &y);
Q1(a, b, y);
} else {
int a;
LL y;
scanf("%d%I64d", &a, &y);
C[get_p(u[a], v[a])] = y;
}
}
return 0;
}