当前位置:网站首页>[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
边栏推荐
- Golang force buckle leetcode 396 Rotation function
- NPM installation yarn
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
- P1390 sum of common divisor (Mobius inversion)
- Flutter 的加载动画这么玩更有趣
- Introduction to sap pi / PO login and basic functions
- SAP debug debug for in, reduce and other complex statements
- [reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
- 亚马逊云科技入门资源中心,从0到1轻松上云
- Your guide to lowering your cholesterol with TLC (continuously updated)
猜你喜欢

ABAP publishes OData service samples from CDs view

Applet error: cannot read property'currenttarget'of undefined

Setnx command execution failed due to full redis memory

Simple understanding of arguments in JS

Three challenges that a successful Devops leader should be aware of

Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
![[educational codeforces round 80] problem solving Report](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[educational codeforces round 80] problem solving Report

Go language learning notes - exception handling | go language from scratch

The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to

JS node operation, why learn node operation
随机推荐
Integral function and Dirichlet convolution
JS scope, scope chain, global variables and local variables
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
SAP ECC connecting SAP pi system configuration
Canary publishing using ingress
653. Sum of two IV - input BST
JS what is an event? Event three elements and operation elements
Acquisition of DOM learning elements JS
云身份过于宽松,为攻击者打开了大门
Number theory blocking (integer division blocking)
Secrets in buffctf file 1
Introduction to sap pi / PO login and basic functions
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
ABAP publishes OData service samples from CDs view
Practice of Flink streaming batch integration in Xiaomi
Redis 异常 read error on connection 解决方案
PHP notes (I): development environment configuration
Kettle experiment conversion case
How to use SQL statement union to get another column of another table when the content of a column in a table is empty