题目链接: 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的更多相关文章

  1. CodeForces 593D Happy Tree Party [LCA+并查集]

    题意:给一棵树,每条边有一个权值,给两种操作,第一种是询问y向下整除从a到b的最短路径中每条边的权值后y的值,第二种是改变某条边的权值. 思路:y的最大值为1e18,最多除大于等于2的数不超过60次即 ...

  2. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  3. 【13.91%】【codeforces 593D】Happy Tree Party

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 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 ...

  5. Codeforces 765 E. Tree Folding

    题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...

  6. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  7. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  8. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. Eclipse + pydev插件

    在Eclipse中安装pydev插件 启动Eclipse, 点击Help->Install New Software...   在弹出的对话框中,点Add 按钮.  Name中填:Pydev,  ...

  2. Node总结

    一. 基本概念 1.1 DOM DOM(Document Object Model), 把网页转换成JS对象,可以用脚本进行各种操作.浏览器将结构化文档(HTML/XML)解析成一系列的节点形成DOM ...

  3. [51nod 1681]公共祖先(dfs序+线段树合并)

    [51nod 1681]公共祖先(dfs序+线段树合并) 题面 给出两棵n(n<=100000)个点的树,对于所有点对求它们在两棵树中公共的公共祖先数量之和. 如图,对于点对(2,4),它们在第 ...

  4. MongoDB数据库-基础篇

    一使用mongodb 1.常用的命令 show dbs    显示数据库列表 use dbname    进入dbname数据库,大小写敏感,没有这个数据库也不要紧 show collections ...

  5. python学习第十天列表的增加,修改,删除操作方法

    在一个有序的数据列表中,集各种数据类型,可以向列表增加元素,也可以修改列表里面的元素,可以删除列表的里面元素,append(),insert(),remove(),pop(),和全局DEL 删除等. ...

  6. Android APP 登陆模块

    首先我想强调一点.这个登陆的模块最好是放在另外一个线程里面来实现.否则有可能会爆出一系列的问题, 然后再与主UI 交互.这样就不会爆ANR异常 1.对于登陆模块的.首先大体的逻辑肯定是要清晰的.    ...

  7. spring boot 配置文件优先级

    项目中可能存在多个配置文件,那么优先级定义如下: 1.同一目录,application.properties优先级高于application.yml 2.同一目录,config文件夹下的配置文件高于根 ...

  8. cdn.bootcss.com无法访问 解决方法

    今天angularjs的网站突然加载报错,提示Refused to execute script from 'https://cdnjs.com/' because its MIME type ('t ...

  9. sessionStorage 使用方法

    作为html5中Web Storage的一种存储方式,localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. W3c上给的介绍是这两者区别在于前者用于持久化的本地 ...

  10. Git相关命令整理

    git config --global user.name  //配置姓名git config --global user.email  //配置邮箱git config --list  //查看配置 ...