CodeForces 593D Happy Tree Party
题目链接: http://codeforces.com/problemset/problem/593/D
---------------------------------------------------------------------------------------------------------------
刚看完题目感觉必须用树链剖分之类的方法,就放弃了,然而我们要发现题目具有如下性质:
一个$ long long $以内的数如果被大于$1$的数除 几十次就可以除到$0$了
所以就可以考虑下$LCA$
但如果长度为$1$的边过多 复杂度又不靠谱了 这时我们又会观察到修改时边的长度只减不增
因此对于长度为$1$的边 我们可以进行路径压缩
不过这里的压缩并不是改变原来的树的结构 因为改变树的结构就会麻烦许多
我们只需把以长度为$1$的边相连的点放在一个集合内, 如果集合内的一个点需要向上查找
就直接找到这个集合的顶端
$($尽管集合顶端的点并不一定是两点的$LCA $但由于集合内的边长均为$1$ 实际结果是等价的$)$
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 2e5 + ;
int firste[N], nexte[N << ], v[N << ];
long long w[N << ];
int fa[N], d[N], aim[N], fa2[N];
long long dist[N];
int n, m, e = ;
void build_edge(int x, int y, long long z)
{
nexte[++e] = firste[x];
firste[x] = e;
v[e] = y;
w[e] = z;
}
int findf(int x)
{
if(fa2[x] == x)
return x;
return fa2[x] = findf(fa2[x]);
}
void dfs(int u)
{
for(int p = firste[u]; p; p = nexte[p])
if(!fa[v[p]])
{
aim[p / ] = v[p];
fa[v[p]] = u;
d[v[p]] = d[u] + ;
dist[v[p]] = w[p];
if(w[p] == )
fa2[v[p]] = findf(u);
dfs(v[p]);
}
}
long long check(int x, int y, long long z)
{
while(x != y)
{
if(d[x] < d[y])
swap(x, y);
if(dist[x] == )
x = findf(x);
else
{
z /= dist[x];
x = fa[x];
if(!z)
return z;
}
}
return z;
}
int main()
{
scanf("%d%d", &n, &m);
int x, y;
long long z;
for(int i = ; i < n; ++i)
{
scanf("%d%d%lld", &x, &y, &z);
build_edge(x, y, z);
build_edge(y, x, z);
}
fa[] = ;
dist[] = ;
for(int i = ; i <= n; ++i)
fa2[i] = i;
dfs();
while(m--)
{
scanf("%d", &x);
if(x & )
{
scanf("%d%d%lld", &x, &y, &z);
printf("%lld\n", check(x, y, z));
}
else
{
scanf("%d%lld", &x, &z);
int u = aim[x];
dist[u] = z;
if(z == )
fa2[u] = findf(fa[u]);
}
}
return ;
}
CodeForces 593D Happy Tree Party的更多相关文章
- CodeForces 593D Happy Tree Party [LCA+并查集]
题意:给一棵树,每条边有一个权值,给两种操作,第一种是询问y向下整除从a到b的最短路径中每条边的权值后y的值,第二种是改变某条边的权值. 思路:y的最大值为1e18,最多除大于等于2的数不超过60次即 ...
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- 【13.91%】【codeforces 593D】Happy Tree Party
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】
Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...
- Codeforces 765 E. Tree Folding
题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- mybatis批量插入、更新和删除
https://blog.csdn.net/m0_37981235/article/details/79131493 https://www.jb51.net/article/132823.htm
- DataContract] [DataMember] 程序集引用
引用,右键->添加引用->System.Runtime.Serialization 添加
- webStom常用快捷键备忘
Ctrl+W 选中代码,连续按会有其他效果 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/…/ ) Ctrl+X 删除行Ctrl+D 复制行 ctrl+shift+ 箭头 上下移动块代 ...
- vue构造器注册UI组件
import ConfirmComponent from '../../components/confirm/index' import { mergeOptions } from '../plugi ...
- (三)Schema与数据类型优化
1.Schema schema,中文叫模式,是数据库的组织和结构 2.选择优化的数据类型 更小的通常更好:尽量使用可以正确存储数据的最小数据类型 简单就好:简单数据类型的操作通常需要更少的cpu周期. ...
- ZOJ 2706 Thermal Death of the Universe
Thermal Death of the Universe Time Limit: 10 Seconds Memory Limi ...
- II play with GG
https://ac.nowcoder.com/acm/contest/338/I 题解:首先轮到出手的时候如果在(0,0)上肯定是输的,而(0,1)(1,0)(0,2)(2,0)(1,1)肯定是赢的 ...
- C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 微信、qq网页二次分享
二次分享是指,在APP或者浏览器分享到微信或者qq,然后从微信或者qq再分享到别的平台.如果不处理,再次分享出去的图片或者标题就不会显示,对用户非常不友好. 一.微信二次分享 官方接入文档:https ...
- JSON对象与JavaScript对象的区别
//js对象的字面量表示法: var people1={ name:'hehe', age:18 }; //json的格式是: var people1={ "name":'hehe ...