题目链接

题意:就是给你一颗这样的树,用一个$y$来除以两点之间每条边的权值,比如$3->7$,问最后的y的是多少,修改操作是把权值变成更小的。

这个$(y<=10^{18})$除的权值如果是$>=2$,那么最多除60几次就变成0了,问题关键是路径上会有好多1存在,这时候我们可以用并查集把他们并到一块,这样就能跳着查了。

具体查法:

从$u$到$LCA(u,v)$,路径上除一遍。

从$v$到$LCA(u,v)$,路径上除一遍。

修改操作如果变成1,就与前面的点合并。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. const int maxn = 2e5 + ;
  8. ///加边
  9. int cnt = , h[maxn];
  10. struct edge
  11. {
  12. int to, pre;
  13. ll v;
  14. } e[maxn << ];
  15. void add(int from, int to, ll v)
  16. {
  17. cnt++;
  18. e[cnt].pre = h[from]; ///5-->3-->1-->0
  19. e[cnt].to = to;
  20. e[cnt].v = v;
  21. h[from] = cnt;
  22. }
  23. ///LCA
  24. int dist[maxn];
  25. int dep[maxn];
  26. int anc[maxn][]; ///2分的父亲节点
  27. ll len[maxn];
  28. void dfs(int u, int fa)
  29. {
  30. for(int i = h[u]; i; i = e[i].pre)
  31. {
  32. int v = e[i].to;
  33. if(v == fa) continue;
  34. dist[v] = dist[u] + e[i].v;
  35. dep[v] = dep[u] + ;
  36. anc[v][] = u;
  37. len[v] = e[i].v;
  38. dfs(v, u);
  39. }
  40. }
  41. void LCA_init(int n)
  42. {
  43. for(int j = ; ( << j) < n; j++)
  44. for(int i = ; i <= n; i++) if(anc[i][j-])
  45. anc[i][j] = anc[anc[i][j-]][j-];
  46. }
  47. int LCA(int u, int v)
  48. {
  49. int log;
  50. if(dep[u] < dep[v]) swap(u, v);
  51. for(log = ; ( << log) < dep[u]; log++);
  52. for(int i = log; i >= ; i--)
  53. if(dep[u] - ( << i) >= dep[v]) u = anc[u][i];
  54. if(u == v) return u;
  55. for(int i = log; i >= ; i--)
  56. if(anc[u][i] && anc[u][i] != anc[v][i])
  57. u = anc[u][i], v = anc[v][i];
  58. return anc[u][];
  59. }
  60. int fa[maxn];
  61. int Find(int x)
  62. {
  63. if(x == fa[x]) return x;
  64. return fa[x] = Find(fa[x]);
  65. }
  66. int main()
  67. {
  68. int n, m; scanf("%d %d", &n, &m);
  69. cnt = ;
  70. for(int i = ; i <= n; i++) h[i] = ;
  71. for(int i = ; i <= n - ; i++)
  72. {
  73. int u, v;
  74. ll w; scanf("%d %d %lld", &u, &v, &w);
  75. add(u, v, w), add(v, u, w);
  76. }
  77. ///LCA初始化
  78. dfs(, );
  79. LCA_init(n);
  80. ///并查集初始化
  81. for(int i = ; i <= n; i++) fa[i] = i;
  82. while(m--)
  83. {
  84. int op, a, b, p;
  85. ll c, y;
  86. scanf("%d", &op);
  87. if(op == )
  88. {
  89. scanf("%d %d %lld", &a, &b, &y);
  90. int fp = LCA(a, b);
  91. a = Find(a);
  92. while(dep[a] > dep[fp] && y > 0LL)
  93. {
  94. if(len[a] == 1LL)
  95. {
  96. fa[a] = anc[a][];
  97. a = Find(a);
  98. }
  99. else
  100. {
  101. y /= len[a];
  102. a = Find(anc[a][]);
  103. }
  104. }
  105. b = Find(b);
  106. while(dep[b] > dep[fp] && y > 0LL)
  107. {
  108. if(len[b] == 1LL)
  109. {
  110. fa[b] = anc[b][];
  111. b = Find(b);
  112. }
  113. else
  114. {
  115. y /= len[b];
  116. b = Find(anc[b][]);
  117. }
  118. }
  119. printf("%lld\n", y);
  120. }
  121. else
  122. {
  123. scanf("%d %lld", &p, &c);
  124. int u = e[p * ].to, v = e[(p * ) ^ ].to;
  125. if(u == anc[v][])
  126. {
  127. len[v] = c;
  128. if(c == 1LL)
  129. {
  130. fa[v] = u;
  131. }
  132. }
  133. else
  134. {
  135. len[u] = c;
  136. if(c == 1LL)
  137. {
  138. fa[u] = v;
  139. }
  140. }
  141. }
  142. }
  143. return ;
  144. }

Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)的更多相关文章

  1. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  2. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  3. Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集

    B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  4. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  5. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  6. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

  7. Codeforces Round #376 (Div. 2) A B C 水 模拟 并查集

    A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

    题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的 ...

  9. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

随机推荐

  1. 如何关闭OSX 10.11 SIP (System Integrity Protection)

    http://www.jianshu.com/p/0572336a0771 注意:SIP功能是Apple在OSX上推出的系统完整性保护功能,对于普通MAC用户来说是一项安全保护功能,如果不了解他的作用 ...

  2. Python基础篇 -- 列表

    3.2 列表的增删改查 ​ 列表使用 [] 来表示,列表中每个元素与元素之间用逗号隔开 ​ 列表也有索引和切片 # 切片切出来的也是列表 lst = ["梅西", "内马 ...

  3. Spring入门Ioc、DI笔记

    Spring是为解决企业应用程序开发复杂性而创建的一个Java开源框架.以下是本人学习过程中总结的一些笔记: 如何学习Spring - 掌握用法 - 深入理解 - 反复总结 - 再次深入理解和实践 s ...

  4. windows10锁定屏幕聚焦图片导出

    打开运行,输入%LocalAppData%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Ass ...

  5. PAT 乙级 1009

    题目 题目地址:PAT 乙级 1009 题解 本题本身属于比较简单的字符串操作题,但是因为对于string的操作和函数不熟悉导致本题做起来很费劲,需要加强对于string类以及相关方法的理解和熟练程度 ...

  6. 前端 MV*模式

    https://github.com/livoras/blog/issues/11 MVC 调用关系如下: Controller(model) ,controller中执行业务逻辑,操作model V ...

  7. getComputedStyle与currentStyle获取元素当前的css样式

    CSS的样式分为三类: 内嵌样式:是写在标签里面的,内嵌样式只对所在的标签有效内部样式:是写在HTML里面的,内部样式只对所在的网页有效外部样式表:如果很多网页需要用到同样的样式,将样式写在一个以.c ...

  8. Day13有参装饰器,三元表达式,匿名函数

    多个装饰器: 加载顺序:由下而上 执行顺序:由上而下 有参装饰器: 闭包,给函数传参的一种方法 当装饰器内需要参数时,可以采用闭包形式给其传参,第三层函数接收完参数时,就变为无参装饰器 三元表达式: ...

  9. Python9- 生成器函数进阶-day14

    生成器进阶#send的获取下一个值的效果和next基本一致,#只不过在获取下一个值的时候,给上一个值的位置穿第一个数据 使用send的注册事项: #第一次使用生成器的时候,必须用next获取下一个值 ...

  10. LeetCode(119) Pascal's Triangle II

    题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...