Summer Training day6 codeforces 593D LCA+并查集

来源:互联网 发布:mac 下载repo 编辑:程序博客网 时间:2024/05/17 07:24

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 ≠ vi1 ≤ 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.

Examples
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以后,我们从两个点出发向LCA开始遍历,把y来除以边上的权值,如果这些权值全都大于等于2的话,那么只需要进行最多64次就可以完成。但是这里有一个可能卡你的地方,那就是链上一堆长度为1得边,最差情况下每次询问都是O(n)的。我们可以这样想,把所有相邻的长度为1的边用并查集合并到一起,这样的话,往上走的过程中就会跳过大量的权值为1的边。注意,当结果为0的时候直接跳出就好了,不必找到LCA,不然还会超时。


其实我觉得还应该可以用树链剖分来做,估计会更简单,有空的话实现一下。

代码:

#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;}





原创粉丝点击