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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Fira Code,可以让不等号!=直接显示出来的字体
今天看B站某直播间有人写代码C#里一堆不等号直接显示,感觉很神奇,以为是插件还是什么新语法,托人问了下原来是Fira Code字体 https://github.com/tonsky/FiraCode ...
- CSAPP:局部性原理
一个编写良好的计算机程序常常具有良好的局部性(locality).局部性通常有两种不同的形式:时间局部性(temporal locality)和空间局部性(spatial locality).在一个具 ...
- 20190814 On Java8 第四章 运算符
第四章 运算符 优先级 程序员经常都会忘记其他优先级规则,所以应该用括号明确规定运算顺序. 赋值 基本类型的赋值都是直接的,而不像对象,赋予的只是其内存的引用.举个例子,a = b ,如果 b 是基本 ...
- 为什么 Kafka 速度那么快?
来源:cnblogs.com/binyue/p/10308754.html Kafka的消息是保存或缓存在磁盘上的,一般认为在磁盘上读写数据是会降低性能的,因为寻址会比较消耗时间,但是实际上,Kafk ...
- [BZOJ 2653] middle(可持久化线段树+二分答案)
[BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...
- each of which 用法
each of which 在以下為 同位語,非關代. 1. An urn contains two balls, each of which is known to be either white ...
- 66.Subarray Sum Equals K(子数组和为K的个数)
Level: Medium 题目描述: Given an array of integers and an integer k, you need to find the total number ...
- rabbitMQ实现推迟队列
一. 使用原生Api 1.RabbitMQ 相关 <dependency> <groupId>com.rabbitmq</groupId> <artifact ...
- R语言ggplot2软件包
相比r语言自带软件包,ggplot2有以下特色 图形语法的核心:统计图形是数据向几何对象属性的一个映射.
- quotaon - 开启关闭文件系统配额
总览 (SYNOPSIS) quotaon [ -e | d ] [ -vug ] filesystem... quotaon [ -e | d ] [ -avug ] quotaoff [ -e | ...