CF208E Blood Cousins(DSU,倍增)
倍增求出祖先,\(\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,倍增)的更多相关文章
- CF208E Blood Cousins
Blood Cousins 题目描述 小C喜欢研究族谱,这一天小C拿到了一整张族谱. 小C先要定义一下k-祖先. x的1-祖先指的是x的父亲 x的k-祖先指的是x的(k-1)-祖先的父亲 小C接下来要 ...
- CF 208E. Blood Cousins [dsu on tree 倍增]
题意:给出一个森林,求和一个点有相同k级祖先的点有多少 倍增求父亲然后和上题一样还不用哈希了... #include <iostream> #include <cstdio> ...
- CF208E Blood Cousins 题解
一个奇奇怪怪的复杂度很垃圾的线段树合并解法 通过分析可以发现,要找$x$的$k$辈兄弟,只需要找到$x$的$k$辈祖先,然后查找以该祖先为根的子树中和$x$深度相同的节点个数$-1$即可.也就是说,询 ...
- CF 208E - Blood Cousins dfs序+倍增
208E - Blood Cousins 题目:给出一棵树,问与节点v的第k个祖先相同的节点数有多少个. 分析: 寻找节点v的第k个祖先,这不就是qtree2简化版吗,但是怎么统计该祖先拥有多少个深度 ...
- [cf contest246] E - Blood Cousins Return
[cf contest246] E - Blood Cousins Return time limit per test 3 seconds memory limit per test 256 meg ...
- Codeforces 246E - Blood Cousins Return (树上启发式合并)
246E - Blood Cousins Return 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor,每个节点有名字,名字不唯一.多次查询,给出 u k ...
- Codeforces 208E - Blood Cousins(树上启发式合并)
208E - Blood Cousins 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor.多次查询,给出 u k,问有多少个与 u 具有相同 k-ance ...
- Codeforces 246E Blood Cousins Return(树上启发式合并)
题目链接 Blood Cousins Return #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) f ...
- CF 246E. Blood Cousins Return [dsu on tree STL]
题意: 一个森林,求k级后代中多少种不同的权值 用set维护每个深度出现的权值 一开始一直在想删除怎么办,后来发现因为当前全局维护的东西里都是当前子树里的,如果要删除那么当前一定是轻儿子,直接清空se ...
随机推荐
- .NET Core 读取配置技巧 - IOptions<TOptions> 接口
原文链接:https://www.cnblogs.com/ysmc/p/16307804.html 在开发过程中,我们无法离开配置文件(appsetting.json),例如配置文件中有以下内容: { ...
- [BZOJ5449] 序列
题目链接:序列 Description 给定一个\(1\)~\(n\)的排列x,每次你可以将 \(x_1, x_2, ..., x_i\) 翻转. 你需要求出将序列变为升序的最小操作次数. 多组数据. ...
- java基础题(3)
5.面向对象 5.1封装 5.1.1修改Data类的定义 描述 现有一个Data类,内部定义了属性x和y,在main方法中实例化了Data类,并计算了data对象中x和y的和.但是,Data类的定义存 ...
- Spark读取elasticsearch数据指南
最近要在 Spark job 中通过 Spark SQL 的方式读取 Elasticsearch 数据,踩了一些坑,总结于此. 环境说明 Spark job 的编写语言为 Scala,scala-li ...
- torch.cat()和torch.stack()
torch.cat() 和 torch.stack()略有不同torch.cat(tensors,dim=0,out=None)→ Tensortorch.cat()对tensors沿指定维度拼接,但 ...
- @vue/cli3+配置build命令构建测试包&正式包
上一篇博客介绍了vue-cli2.x配置build命令构建测试包和正式包,但现在前端开发vue项目大多数使用新版@vue/cli脚手架搭建vue项目(vue create project-name) ...
- iPhone x 的区别
最近入手两台iPhone x, 均从官网购买,两台分别是2017年和2018年生产,对比了一下,两台还有是一些差别: 首先苹果X使用起来还是非常爽的,没有HOME键,明显比按HOME键方便,因为按HO ...
- Vue是怎么渲染template内的标签内容的?
我们在使用Vue做项目时,都会用到脚手架,相应的我们会在template写标签内容.那么你知道为什么会在template写标签吗?这当中经过了怎样的处理呢? <template> < ...
- python这不是有手就行?——python音频处理基础知识
大家应该都知道声音的基础吧? 啊不知道当我没说吧~~~ 1.声音的基础 2.python读取.wav音频 Python学习交流Q群:660193417#### import wave import s ...
- 『现学现忘』Docker基础 — 41、将本地镜像推送到阿里云
目录 1.准备工作 2.阿里云容器镜像仓库的使用 (1)创建命名空间 (2)创建容器镜像 (3)查看阿里云镜像仓库的信息 3.将本地Docker镜像推送到阿里云 (1)登陆阿里云 (2)给镜像生成版本 ...