[codeforces208E]Blood Cousins(dsu on the tree+倍增)
来源:互联网 发布:揭阳网络广告公司 编辑:程序博客网 时间:2024/05/29 04:33
题目:
我是超链接
题意:
给出一棵家谱树,定义向上走k步到达的节点为该点的k-ancestor。每次询问与v同P-ancestor的节点有多少个。
题解:
和上道题只有一个区别嘛,你建立虚树的时候还需要跳到相应的高度,找到相应的父亲,然后问题是:深度为h[v]的结点个数
代码:
#include <cstdio>#define N 100005#define sz 19 using namespace std;int tot,nxt[N*2],point[N],v[N*2],tot1,nxt1[N*2],point1[N],v1[N*2],id[N];int ans[N],f[N][sz],h[N],size[N],son[N],mi[sz],cnt[N],Son;bool vis[N];void addline(int x,int y){++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;}void addline1(int x,int y,int i){++tot1; nxt1[tot1]=point1[x]; point1[x]=tot1; v1[tot1]=y; id[tot1]=i;}void getson(int x,int fa){ size[x]=1;h[x]=h[fa]+1; for (int i=1;i<sz;i++) if (h[x]<mi[i]) break; else f[x][i]=f[f[x][i-1]][i-1]; for (int i=point[x];i;i=nxt[i]) { f[v[i]][0]=x; getson(v[i],x); if (size[v[i]]>size[son[x]]) son[x]=v[i]; size[x]+=size[v[i]]; }}int find(int x,int k){ for (int i=sz-1;i>=0;i--) if ((k>>i)&1) x=f[x][i]; return x;}void add(int x,int vv){ cnt[h[x]]+=vv; for (int i=point[x];i;i=nxt[i]) if (v[i]!=Son) add(v[i],vv);}void dfs(int x,int k){ vis[x]=1; for (int i=point[x];i;i=nxt[i]) if (v[i]!=son[x]) dfs(v[i],0); if (son[x]) dfs(son[x],1),Son=son[x]; add(x,1); Son=0; for (int i=point1[x];i;i=nxt1[i]) ans[id[i]]=cnt[v1[i]]-1; if (!k) add(x,-1);}int main(){ int n,i,m; mi[0]=1; for (i=1;i<sz;i++) mi[i]=mi[i-1]*2; scanf("%d",&n); for (i=1;i<=n;i++) { int fa; scanf("%d",&fa); if (fa) addline(fa,i); } for (i=1;i<=n;i++) if (!size[i]) getson(i,0); scanf("%d",&m); for (i=1;i<=m;i++) { int vv,hh; scanf("%d%d",&vv,&hh); int fa=find(vv,hh); addline1(fa,h[vv],i); } for (i=1;i<=n;i++) if (!vis[i]) dfs(i,0); for (i=1;i<=m;i++) printf("%d ",ans[i]);}
阅读全文
0 0
- [Codeforces208E]Blood Cousins(dsu on the tree+倍增)
- [codeforces208E]Blood Cousins(dsu on the tree+倍增)
- codeforces 208 E. Blood Cousins (dsu on the tree)
- [Codeforces246E]Blood Cousins Return(dsu on the tree+set)
- [codeforces246E]Blood Cousins Return(dsu on the tree+STL)
- codeforces 246 E. Blood Cousins Return (set+dsu on the tree)
- codeforces 570 D. Tree Requests (dsu on the tree)
- [Codeforces570D]Tree Requests(dsu on the tree)
- [Codeforces375D]Tree and Queries(dsu on the tree+bit)
- [codeforces570D]Tree Requests(dsu on the tree)
- [codeforces375D]Tree and Queries(dsu on the tree+bit)
- codeforces 600 E. Lomsat gelral (dsu on the tree)
- [Codeforces600E]Lomsat gelral(dsu on the tree)
- [codeforces600E]Lomsat gelral(dsu on the tree+讲解)
- dsu on the tree 学习笔记
- [trick]dsu on tree
- dsu on tree
- [trick]dsu on tree
- 使用Spring Cloud和Docker构建微服务架构
- 多道批处理系统、分时操作系统
- 动态规划-123. Best Time to Buy and Sell Stock III
- Java 基础(1)—— public class 与 class 的区别
- 逆欧拉函数[arc]
- [codeforces208E]Blood Cousins(dsu on the tree+倍增)
- hdu 4821 String(hash)
- abc
- Android O中Notification 的修改与其使用错误
- what a fuck!这是什么鬼东西?
- UVA
- 欧拉函数求和
- leetcode 363. Max Sum of Rectangle No Larger Than K 动态规划DP + 暴力循环
- POJ 2186 Popular Cows 笔记