题目链接

题意:就是给你一颗这样的树,用一个$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+并查集)的更多相关文章

  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. php微信开发自动回复一直提示“该公众号提供的服务出现故障,请稍后再试”

    坑:服务器可以接受到发到公众号的信息,但是公众号不能回复,直接echo " ";exit();也会提示“该公众号提供的服务出现故障,请稍后再试”: 可能原因:用的php,是把数组转 ...

  2. shell脚本,提取ip地址和子网掩码,和查外网ip地址信息。

        #提取IP地址和子网掩码 [root@localhost ~]# ifconfig eth0|grep 'inet addr'|awk -F'[ :]+' '{print $4"/& ...

  3. 什么是无符号段整数,什么又是有符号数,(c++与java语言里边的不同)

    c++中:整型数分为有符号数和无符号数两种 unsigned int a;无符号整型变量a,意思是这个数最小值为0,最大值为2的32次方-1,因为一个整型数占四个字节,一个字节8位,共32位 int ...

  4. CS193p Lecture 7 - Views, Gestures

    Views 如何绘制自定义图像 Gestures 如何处理用户手势操作 Views 1.它是基本的构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,在此空间中可以绘制,可以添加触控事件: 2.它是分 ...

  5. noip_最后一遍_1-数学部分

    它就是要来了 noip数论一般会以三种形式呈现 注 码风可能有些毒 (有人说我压行qwq) 大概保持标准三十五行左右 为什么是三十五行呢 因为我喜欢这个数字 我喜欢三十五而已(足球球衣也会用这个号哒) ...

  6. C++系统学习之四:数组

    与vector的异同 相同:都是存放类型相同对象的容器 不同:数组的大小确定不变,不能随意向数组中增加元素 1.定义和初始化内置数组 数组中元素的个数也属于数组类型的一部分,编译的时候维度应该是已知的 ...

  7. 解决zend studio代码无法自动提示的3个方法

    最近电脑重装,索性把用了好多年的老版本7.x 升级了,网上下载了一个12.x的破解版. 起初一切正常,等导入项目开始开发的时候发现PHP函数尽然没有提示,一脸懵逼! 经过多方查阅和尝试,现在分享3个解 ...

  8. python logging with yaml

    Recently, I was made a service which can provide a simple way to get best model. so, i spent lot of ...

  9. Mac VMware fusion nat 外网映射

    当我们在使用VMware fusion NAT模式时,相当于形成了一个虚拟的局域网VLAN,这时虚拟机可以对外通信,但是nat对外隐藏了内网,外网访问虚拟机的时候就会遇到问题,比如ping ,ssh ...

  10. Knockout v3.4.0 中文版教程-15-控制文本内容和外观-attr绑定

    6. attr绑定 目的 attr绑定可以给关联DOM元素的任何属性赋值.这个绑定很棒,比如,当你想要设置通过视图模型给元素的title属性.img标签的src属性或超链接的href值,当视图模型对应 ...