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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- 操作系统 - Linux命令整理 - Ubuntu
镜像 http://mirrors.163.com/ubuntu-releases/ 系统相关 Ubuntu14.04相关 安装 - VMware Install Ubuntu Continue In ...
- Hexo-博客yilia主题创建分类(categories)和标签(tags)首页
转载自:http://orzcss.com/posts/5a207d64/ 概述 默认安装的 hexo 本身是没有分类和标签首页的,例如:http://orzcss.com/categories/页面 ...
- 设置div标签可以输入文字
1.contenteditable 属性可以设置div标签为克输入标签, 2.input和textarea虽然是常用的输入标签,但是这两个标签不能设置最大高度和最小高度, 随意如果想随着输入的内容 ...
- project euler-34
145是个奇怪的数字.由于1!+ 4! + 5! = 1 + 24 + 120 = 145. 请求出能表示成其每位数的阶乘的和的全部数的和. 请注意:由于1! = 1和2! = 2不是和,故它们并不包 ...
- ubuntu安装supervisor以及使用supervisor启动.net core进程
1.下载.net core项目ubuntu系统运行容器dotnet 1.版本:dotnet-sdk-2.1.3-linux-x64.tar.gz 2.将下载好的包上传到ubuntu ...
- 循环冗余校验(CRC)
冗余码 CRC和海明校验类似,也是有效信息(k位)+校验信息(r位),需要满足N=k+r≤2r-1 生成多项式G(X) 定义:收发双方约定的一个(r+1)位二进制数,发送方利用G(X)对信息多项式做模 ...
- rtp/rtcp
rtp/rtcp stack custom rtp ORTP UCL Common RTP library Bell Labs RTP Library jrtplib 1.custom rtp sen ...
- Informatica参考
近日在做informatica项目时,要求参照informatica中原有的mapping方式实现.那么当知道源表或者目标表或者是映射时,如何快速的找出分散在不同的Subject的mapping或者是 ...
- Python错误提示:[Errno 24] Too many open files的分析与解决
背景 最近在工作中发现了一个错误,在执行多线程扫描脚本的时候频繁出现下面这个错误 HTTPConnectionPool(host=‘t.tips', port=80): Max retries exc ...
- ltp-ddt nand_ecc_tests
NAND_S_FUNC_ECC_2K_BCH8_8ERRS_NO_OOB_ERR source "common.sh"; nandecc_tests.sh -r "0:0 ...