![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
lca
wspl654321
。。。。。。
展开
-
lca最小公共祖先祖先
#include <cstdio> #include<cstring> using namespace std; int du[99999]; int flag[2999]; double dis[2099]; int w[2989][2999]; int n,m; int main() { int x,y,z; scanf("%d%d",&n,&m); memset(dis,原创 2017-03-05 11:18:49 · 315 阅读 · 0 评论 -
noip2013
day1 t1 转圈游戏(circle) 裸地快速幂。。#include<cstdio> using namespace std; int n,m,x,k; int fastpow(int a,int k){ int ans=1; while(k){ if(k&1) ans=(1ll*ans*a)%n; a=(1ll*a*a)%n;原创 2017-10-18 14:46:09 · 387 阅读 · 0 评论 -
运输计划
这个题和疫情控制差不多,但那个题更恶心。 先二分答案,找需要减少的边,看他们有没有一条边是一样的 如果没有的话,肯定不行,如果有的话,看删掉最大的能不能让所有的路大于答案 树上的最短边就是到公共祖先的距离,这里用tarjan求得,也可以用倍增,不会的话看前面的博客 找一条边经过多少次用差分 把两个节点+一就代表这两个叉上的点都走过一遍,可是祖先上面的点没有走,所以把祖先-2,求树的后缀和就原创 2017-10-23 11:03:59 · 661 阅读 · 0 评论 -
codevs 2370小机房的树
先找到他们的最近公共祖先,记下他们的深度(就是花费) 最后的最小花费就是询问的减祖先加起来#include<cstdio> #include<iostream> using namespace std; const int M=1001009; int n,m,s,f[M],vis[M],lca[M*2];int nex[M*2],head[M*2],to[M*2],tot,heade[M*2],原创 2017-10-16 16:19:08 · 275 阅读 · 0 评论 -
dfs(tarjan)求lca
离线做法,可以做到o(n) 对于每一棵树,向下搜索,变成小树,如果子节点没有子节点了,就返回 然后在每一步中,找有x的查询,如果另一个访问过,就是他们的祖先 不同颜色的就是不同的子树 #include<cstdio> #include<iostream> using namespace std; const int M=1001009; int n,m,s,f[M],vis[M],lca[M原创 2017-10-16 15:58:52 · 237 阅读 · 0 评论