codeforces 593 D. Happy Tree Party (LCA + 并查集)

来源:互联网 发布:sem与seo的区别 编辑:程序博客网 时间:2024/06/06 07:42
D. Happy Tree Party
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

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

Input

The first line of the input contains integers, n and m (2 ≤ n ≤ 200 0001 ≤ 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 uivi and xi (1 ≤ ui, vi ≤ nui ≠ 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.
It is guaranteed that all the queries are correct, namely 1 ≤ ai, bi ≤ n1 ≤ pi ≤ n - 11 ≤ yi ≤ 1018 and 1 ≤ ci < xpi, where xpi represents a number written on edge pi at this particular moment of time that is not necessarily equal to the initial value xpi, as some decreases may have already been applied to it. The edges are numbered from 1 to n - 1 in the order they appear in the input.
Output

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.

Sample test(s)
input
6 61 2 11 3 71 4 42 5 52 6 21 4 6 172 3 21 4 6 171 5 5 202 4 11 5 1 3
output
24203
input
5 41 2 71 3 33 4 23 5 51 4 2 1001 5 4 12 2 21 1 3 4
output
202

树上的路径,少不了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 secondconst 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;}


0 0