Summer Training day6 codeforces 593D LCA+并查集
来源:互联网 发布:mac 下载repo 编辑:程序博客网 时间:2024/05/17 07:24
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 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
24203
5 41 2 71 3 33 4 23 5 51 4 2 1001 5 4 12 2 21 1 3 4
202
其实我觉得还应该可以用树链剖分来做,估计会更简单,有空的话实现一下。
代码:
#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;}
- Summer Training day6 codeforces 593D LCA+并查集
- Summer training day6 codeforces19E 树链剖分,并查集
- codeforces 593D(并查集 + LCA)
- Summer Training day6 codeforces292D并查集合并
- Summer Training day6 codeforces 675D 二叉搜索树
- codeforces 593 D. Happy Tree Party (LCA + 并查集)
- codeforces 593D Happy Tree Party Lca+并查集
- CodeForces 593D Happy Tree Party(树链剖分(边权) or LCA+并查集)
- codeforces #329 D. Happy Tree Party (LCA+并查集 || 树链剖分)
- Codeforces Round #329 (Div. 2)D LCA+并查集路径压缩
- Summer Training day6 codeforces633G 线段树、bitset
- Codeforces 366D 贪心+并查集
- Codeforces 371D. Vessels【并查集】
- codeforces 366D 并查集
- codeforces 691D 并查集
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 766D 并查集
- codeforces 766D 并查集进阶
- 输油管道问题
- 1037. Magic Coupon (25)
- poj 3523 uva 3523
- SAP_BW建模第四步之创建转换规则(Transformation)
- Python基础总结(2)
- Summer Training day6 codeforces 593D LCA+并查集
- 安卓工作室 创建java工程 程序
- poj-3050
- 笔试笔记(三)
- 跳棋
- 【HDU 6103 Kirinriki】 & 尺取
- 百度api算面积
- 1038. Recover the Smallest Number (30)
- JSP内置对象和作用