Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)
题目链接
题意:就是给你一颗这样的树,用一个$y$来除以两点之间每条边的权值,比如$3->7$,问最后的y的是多少,修改操作是把权值变成更小的。
这个$(y<=10^{18})$除的权值如果是$>=2$,那么最多除60几次就变成0了,问题关键是路径上会有好多1存在,这时候我们可以用并查集把他们并到一块,这样就能跳着查了。
具体查法:
从$u$到$LCA(u,v)$,路径上除一遍。
从$v$到$LCA(u,v)$,路径上除一遍。
修改操作如果变成1,就与前面的点合并。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std; typedef long long ll;
const int maxn = 2e5 + ;
///加边
int cnt = , h[maxn];
struct edge
{
int to, pre;
ll v;
} e[maxn << ];
void add(int from, int to, ll v)
{
cnt++;
e[cnt].pre = h[from]; ///5-->3-->1-->0
e[cnt].to = to;
e[cnt].v = v;
h[from] = cnt;
}
///LCA
int dist[maxn];
int dep[maxn];
int anc[maxn][]; ///2分的父亲节点
ll len[maxn];
void dfs(int u, int fa)
{
for(int i = h[u]; i; i = e[i].pre)
{
int v = e[i].to;
if(v == fa) continue;
dist[v] = dist[u] + e[i].v;
dep[v] = dep[u] + ;
anc[v][] = u;
len[v] = e[i].v;
dfs(v, u);
}
}
void LCA_init(int n)
{
for(int j = ; ( << j) < n; j++)
for(int i = ; i <= n; i++) if(anc[i][j-])
anc[i][j] = anc[anc[i][j-]][j-];
}
int LCA(int u, int v)
{
int log;
if(dep[u] < dep[v]) swap(u, v);
for(log = ; ( << log) < dep[u]; log++);
for(int i = log; i >= ; i--)
if(dep[u] - ( << i) >= dep[v]) u = anc[u][i];
if(u == v) return u;
for(int i = log; i >= ; i--)
if(anc[u][i] && anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
return anc[u][];
}
int fa[maxn];
int Find(int x)
{
if(x == fa[x]) return x;
return fa[x] = Find(fa[x]);
}
int main()
{
int n, m; scanf("%d %d", &n, &m);
cnt = ;
for(int i = ; i <= n; i++) h[i] = ;
for(int i = ; i <= n - ; i++)
{
int u, v;
ll w; scanf("%d %d %lld", &u, &v, &w);
add(u, v, w), add(v, u, w);
}
///LCA初始化
dfs(, );
LCA_init(n);
///并查集初始化
for(int i = ; i <= n; i++) fa[i] = i;
while(m--)
{
int op, a, b, p;
ll c, y;
scanf("%d", &op);
if(op == )
{
scanf("%d %d %lld", &a, &b, &y);
int fp = LCA(a, b);
a = Find(a);
while(dep[a] > dep[fp] && y > 0LL)
{
if(len[a] == 1LL)
{
fa[a] = anc[a][];
a = Find(a);
}
else
{
y /= len[a];
a = Find(anc[a][]);
}
}
b = Find(b);
while(dep[b] > dep[fp] && y > 0LL)
{
if(len[b] == 1LL)
{
fa[b] = anc[b][];
b = Find(b);
}
else
{
y /= len[b];
b = Find(anc[b][]);
}
}
printf("%lld\n", y);
}
else
{
scanf("%d %lld", &p, &c);
int u = e[p * ].to, v = e[(p * ) ^ ].to;
if(u == anc[v][])
{
len[v] = c;
if(c == 1LL)
{
fa[v] = u;
}
}
else
{
len[u] = c;
if(c == 1LL)
{
fa[u] = v;
}
}
}
}
return ;
}
Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)的更多相关文章
- 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 ...
- 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 ...
- 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/ ...
- 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 ...
- 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 ...
- 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. ...
- 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 ...
- Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)
题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂 总共有m对试剂能反应,按不同的 ...
- Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集
题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...
随机推荐
- caffe的调试技巧 和 使用split层
1.网络中的layer层的输出,只要没有作为其他层的输入,caffe的日志就会把这个top输出(如果你用那个网站画网络结构图,你也会发现这种情况的层的颜色是不一样的,是紫色的) 2.如果你想看某一层在 ...
- 新数据的GT列表
制作新数据集时需要重新制作train_GT,test_GT 代码: dic = {} with open('/home/bnrc/all_image_GT.txt','r') as file: for ...
- 数据库-SQL语法:LEFT JOIN
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.(补充:left join是一对多的关系,表里所有符合条件的记 ...
- python 实现计算器功能 输入字符串,输出相应结果
import re formul='1 - 2 *( (6 0- 30+(0-40/5) * (9-2* 5/3 +7 /3*99/4*2998 +10 *568/14)) - (-4*3) / (1 ...
- 小数据池 is 和 ==的区别
小数据池 一.小数据池 1)代码块 python程序是由代码块构成的,一个代码块的文本作为pythont程序执行的单元 官方文档: A Python program is constructed fr ...
- springboot elk实时日志搭建
https://blog.csdn.net/yy756127197/article/details/78873310 基本的上的过程如这篇博客,logback的配置文件和依赖不太一样 具体见源码其中的 ...
- STM32F407 DAC 个人笔记
DAC框图 VDDA:模拟电源输入 VSSAL:模拟电源接地输入 Vref+:正模拟参考电压输入 -------------- DORx:输入数字寄存器 DAC_OUT:模拟输出通道 DAC_OUT1 ...
- python+selenium 鼠标事件操作
一.前言 除了可以使用 click( ) 来模拟鼠标的单击操作,现在Web产品中还提供了更丰富的鼠标交互方式,例如鼠标右键.双击.悬停.拖动等功能,在WebDriver中,将这些关于鼠标操作的方法都封 ...
- selenium之定位以及切换frame
总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切,frame需层层切! 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug ...
- Python第三方库之openpyxl(6)
Python第三方库之openpyxl(6) 折线图 折线图允许在固定轴上绘制数据,它们类似于散列图,主要的区别在于,在折线图中,每个数据序列都是根据相同的值绘制的,不同的轴可以用于辅助轴,与条形图类 ...