倍增求出祖先,\(\text{DSU}\)统计

本来想用树剖求\(K\)祖,来条链复杂度就假了

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define MP make_pair
#ifdef QWQ
#define D_e_Line printf("\n------\n")
#define D_e(x) cerr << (#x) << " " << x << endl
#define C_e(x) cout << (#x) << " " << x << endl
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <cassert>
#define PASS fprintf(stderr, "Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#else
#define D_e_Line
#define D_e(x)
#define C_e(x)
#define FileOpen()
#define FileSave()
#define Pause()
#define PASS
#endif
using namespace std;
struct FastIO {
template<typename ATP> inline FastIO& operator >> (ATP &x) {
x = 0; int sign = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') sign = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
if(sign == -1) x = -x;
return *this;
}
} io;
template<typename ATP> inline ATP Max(ATP x, ATP y) {
return x > y ? x : y;
}
template<typename ATP> inline ATP Min(ATP x, ATP y) {
return x < y ? x : y;
}
template<typename ATP> inline ATP Abs(ATP x) {
return x < 0 ? -x : x;
}
#include <vector>
const int N = 1e5 + 7;
struct Edge {
int nxt, pre;
} e[N];
int head[N], cntEdge;
inline void add(int u, int v) {
e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
vector<pair<int, int>> q[N];
int dep[N], fa[N][19], siz[N], top[N], dfn[N], dfnIdx, rnk[N], son[N];
void DFS_First(int u, int father) {
dep[u] = dep[father] + 1, fa[u][0] = father, siz[u] = 1;
R(i,1,18){
if(fa[u][i - 1]) fa[u][i] = fa[fa[u][i - 1]][i - 1];
else break;
}
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
DFS_First(v, u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]]) son[u] = v;
}
}
void DFS_Second(int u, int Tp) {
top[u] = Tp, dfn[u] = ++dfnIdx, rnk[dfnIdx] = u;
if(!son[u]) return;
DFS_Second(son[u], Tp);
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v != son[u]) DFS_Second(v, v);
}
}
inline int Father(int u, int K) {
// while(dep[u] - dep[top[u]] + 1 <= K){
// K -= dep[u] - dep[top[u]] + 1;
// u = fa[top[u]];
// }
// return rnk[dfn[u] - K]; while(K){
int now = 0;
while((1 << (now + 1)) <= K) ++now;
u = fa[u][now];
K -= 1 << now;
}
return u;
}
int tot[N];
bool big[N];
void Add(int u) {
++tot[dep[u]];
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(big[v]) continue;
Add(v);
}
}
void Del(int u) {
--tot[dep[u]];
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(big[v]) continue;
Del(v);
}
}
int ans[N], n;
void DSU(int u, bool flag) {
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == son[u]) continue;
DSU(v, 0);
}
if(son[u]) DSU(son[u], 1), big[son[u]] = true;
Add(u);
for(vector<pair<int, int>>::iterator it = q[u].begin(); it != q[u].end(); ++it){
if(it->first + dep[u] <= n + 1){
ans[it->second] = tot[it->first + dep[u]] - 1;
}
}
big[son[u]] = false;
if(!flag) Del(u);
}
int main() {
int Q;
io >> n;
R(i,1,n){
int fat;
io >> fat;
++fat;
add(fat, i + 1);
}
DFS_First(1, 0);
DFS_Second(1, 1);
// while(1){
// int x, K;
// io >> x >> K;
// ++x;
// cout << Father(x, K) - 1<< endl;
// }
io >> Q;
R(i,1,Q){
int x, K;
io >> x >> K;
++x;
x = Father(x, K);
if(x == 1){
continue;
}
// D_e(x - 1);
q[x].emplace_back(K, i);
}
DSU(1, 0); R(i,1,Q){
printf("%d ", ans[i]);
}
return 0;
}
/*
6
0 1 1 0 4 4
7
1 1
1 2
2 1
2 2
4 1
5 1
6 1
*/
/*
6
0 1 2 3 4 5
5 1
*/

CF208E Blood Cousins(DSU,倍增)的更多相关文章

  1. CF208E Blood Cousins

    Blood Cousins 题目描述 小C喜欢研究族谱,这一天小C拿到了一整张族谱. 小C先要定义一下k-祖先. x的1-祖先指的是x的父亲 x的k-祖先指的是x的(k-1)-祖先的父亲 小C接下来要 ...

  2. CF 208E. Blood Cousins [dsu on tree 倍增]

    题意:给出一个森林,求和一个点有相同k级祖先的点有多少 倍增求父亲然后和上题一样还不用哈希了... #include <iostream> #include <cstdio> ...

  3. CF208E Blood Cousins 题解

    一个奇奇怪怪的复杂度很垃圾的线段树合并解法 通过分析可以发现,要找$x$的$k$辈兄弟,只需要找到$x$的$k$辈祖先,然后查找以该祖先为根的子树中和$x$深度相同的节点个数$-1$即可.也就是说,询 ...

  4. CF 208E - Blood Cousins dfs序+倍增

    208E - Blood Cousins 题目:给出一棵树,问与节点v的第k个祖先相同的节点数有多少个. 分析: 寻找节点v的第k个祖先,这不就是qtree2简化版吗,但是怎么统计该祖先拥有多少个深度 ...

  5. [cf contest246] E - Blood Cousins Return

    [cf contest246] E - Blood Cousins Return time limit per test 3 seconds memory limit per test 256 meg ...

  6. Codeforces 246E - Blood Cousins Return (树上启发式合并)

    246E - Blood Cousins Return 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor,每个节点有名字,名字不唯一.多次查询,给出 u k ...

  7. Codeforces 208E - Blood Cousins(树上启发式合并)

    208E - Blood Cousins 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor.多次查询,给出 u k,问有多少个与 u 具有相同 k-ance ...

  8. Codeforces 246E Blood Cousins Return(树上启发式合并)

    题目链接 Blood Cousins Return #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) f ...

  9. CF 246E. Blood Cousins Return [dsu on tree STL]

    题意: 一个森林,求k级后代中多少种不同的权值 用set维护每个深度出现的权值 一开始一直在想删除怎么办,后来发现因为当前全局维护的东西里都是当前子树里的,如果要删除那么当前一定是轻儿子,直接清空se ...

随机推荐

  1. Fail2ban 配置详解 基础配置(fail2ban.conf)

    [Definition] loglevel = INFO # 设置日志级别:级别越低显示日志的信息更详细. # CRITICAL - 关键级别 # ERROR - 错误级别 # WARNING - 警 ...

  2. MySQL - 数据库设计步骤

    需求分析:分析用户的需求,包括数据.功能和性能需求. 概念结构设计:主要采用E-R模型进行设计,包括画E-R图. 逻辑结构设计:通过将E-R图转换成表,实现从E-R模型到关系模型的转换,进行关系规范化 ...

  3. pycharm编辑器下载与安装

    pycharm编辑器下载与安装 首先是下载地址:https://www.jetbrains.com/pycharm/download/#section=windows   首先根据自己的电脑选择系统, ...

  4. 3. Docker应用

  5. 论文阅读 Real-Time Streaming Graph Embedding Through Local Actions 11

    9 Real-Time Streaming Graph Embedding Through Local Actions 11 link:https://scholar.google.com.sg/sc ...

  6. AsList()方法详解

    AsList()方法详解 在Java中,我们应该如何将一个数组array转换为一个List列表并赋初始值?首先想到的肯定是利用List自带的add()方法,先new一个List对象,再使用add()方 ...

  7. 透过Redis源码探究字符串的实现

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的Redis 5.0源码 概述 最近在通过 Redis 学 C 语言,不得不说, ...

  8. Python之枚举法解数学题

    作为初二的学生,数学题总是令我苦恼的问题.尤其是我们这里的预备班考试(即我们这里最好的两所高中提前一年招生,选拔尖子生的考试)将近,我所面对的数学题越发令人头疼. 这不,麻烦来了: 如图,在正方形AB ...

  9. 链表设计与Java实现,手写LinkedList这也太清楚了吧!!!

    链表设计与实现 在谈链表之前,我们先谈谈我们平常编程会遇到的很常见的一个问题.如果在编程的时候,某个变量在后续编程中仍需使用,我们可以用一个局部变量来保存该值,除此之外一个更加常用的方法就是使用容器了 ...

  10. 【docker专栏1】docker解决的实际问题及应用场景

    Docker是一个开源的容器引擎,它轻巧,且易移植,"build once, configure once and run anywhere".使用go语言开发,并遵从apache ...