当前位置:网站首页>[codeforces - 208e] blood cousins
[codeforces - 208e] blood cousins
2022-04-23 09:43:00 【Zimba_】
Topic link
Enhanced example ( Not yet )
The question :
Given a tree n n n A tree of nodes . give m m m Time to ask , Give out... Every time you ask x x x and k k k, ask x x x How many k k k Generation brother .( The two points are k k k The definition of generation brothers is their k k k Same level ancestors )
1 ≤ n , m ≤ 1 0 5 1\leq n,m \leq 10^{5} 1≤n,m≤105
practice :
Take depth as the first keyword for all points , d f s dfs dfs The order is the second keyword . then k k k Brother Dai must be in a continuous interval , In this way, it is transformed into an interval problem .
In this case , After arranging the order , Divide the left and right boundaries of inquiry point two , You can find all of it k k k Generation brothers .
( The enhanced version is for k k k The third generation of brothers k k k Big , Just use the chairman tree to maintain the interval )
Code :
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int>v[100005];
int id[100005],idx,dep[100005],fa[100005][20];
void dfs(int x,int pre)
{
if(x==0)dep[x]=0,fa[x][0]=0;
else dep[x]=dep[pre]+1,fa[x][0]=pre;
if(x!=0)id[x]=++idx;
for(auto to:v[x])
{
if(to!=pre)
{
dfs(to,x);
}
}
}
bool cmp(int a,int b)
{
if(dep[a]!=dep[b])return dep[a]<dep[b];
else return id[a]<id[b];
}
int getK(int x,int k)
{
for(int i=0;i<=18;i++)if((k>>i)&1)x=fa[x][i];
return x;
}
int idd[100005];
int wei[100005];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
idd[i]=i;
int x;
scanf("%d",&x);
v[x].push_back(i);
v[i].push_back(x);
}
dfs(0,-1);
for(int i=1;i<=18;i++)
{
for(int j=1;j<=n;j++)fa[j][i]=fa[fa[j][i-1]][i-1];
}
sort(idd+1,idd+n+1,cmp);
for(int i=1;i<=n;i++)wei[idd[i]]=i;
int m;
scanf("%d",&m);
while(m--)
{
int x,k;
scanf("%d%d",&x,&k);
int f=getK(x,k);
if(f==0){
printf("%d\n",0);continue;}
int led,red;
int l=1,r=wei[x];
while(l<r)
{
int p=(l+r)/2;
if(getK(idd[p],k)==f)r=p;
else l=p+1;
}
led=l;
l=wei[x],r=n;
while(l<r)
{
int p=(l+r+1)/2;
if(getK(idd[p],k)==f)l=p;
else r=p-1;
}
red=l;
printf("%d\n",red-led+1-1);
}
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621425066.html
边栏推荐
- php 二维数组指定元素相等后相加否则新增
- Leetcode0587. 安装栅栏(difficult)
- Less than 100 secrets about prime numbers
- JS what is an event? Event three elements and operation elements
- Practice of Flink streaming batch integration in Xiaomi
- Es aggregation aggregation analysis
- NLLLoss+log_ SoftMax=CE_ Loss
- [geek challenge 2019] havefun1
- 501. Mode in binary search tree
- Redis expired key cleaning and deletion policy summary
猜你喜欢

Introduction to sap pi / PO login and basic functions

Where is int a = 1 stored

Simple understanding of arguments in JS

Flink 流批一体在小米的实践
![3、 6 [Verilog HDL] gate level modeling of basic knowledge](/img/36/46f2413ecb12f81c003848c93f6bc9.jpg)
3、 6 [Verilog HDL] gate level modeling of basic knowledge

Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice

Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '

Leetcode question bank 78 Subset (recursive C implementation)

《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路

Redis 内存占满导致的 Setnx 命令执行失败
随机推荐
Dropout技术之随机神经元与随机深度
ABAP CDs view with association example
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
GUI, CLI and UNIX Philosophy
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
Kettle experiment (III)
Kettle实验 转换案例
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
Leetcode0587. 安装栅栏(difficult)
Golang force buckle leetcode 396 Rotation function
代码源每日一题 div1 (701-707)
Go language learning notes - language interface | go language from scratch
P1390 sum of common divisor (Mobius inversion)
JS DOM learn three ways to create elements
ABAP publishes OData service samples from CDs view
Compile and debug mysql8 with clion under MacOS x
Random neurons and random depth of dropout Technology
Three ways to create objects in JS
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
Kettle实验