当前位置:网站首页>[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
边栏推荐
- MySQL of database -- basic common query commands
- [geek challenge 2019] havefun1
- Canary publishing using ingress
- Little girl walking
- Codeforces Round #784 (Div. 4)
- SAP pi / PO soap2proxy consumption external WS example
- kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby
- Three ways to create objects in JS
- Redis 内存占满导致的 Setnx 命令执行失败
- 代码源每日一题 div1 (701-707)
猜你喜欢
Number theory blocking (integer division blocking)
Kettle experiment conversion case
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
[geek challenge 2019] havefun1
How to obtain geographical location based on photos and how to prevent photos from leaking geographical location
Easy to understand subset DP
What is monitoring intelligent playback and how to use intelligent playback to query video recording
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
NLLLoss+log_ SoftMax=CE_ Loss
Canary publishing using ingress
随机推荐
云身份过于宽松,为攻击者打开了大门
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Two declaration methods of functions of JS
SAP pi / PO function operation status monitoring and inspection
Single sign on SSO
Es aggregation aggregation analysis
Chapter VIII project stakeholder management of information system project manager summary
Kettle实验 (三)
Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail
Leetcode0587. Install fence
Creation of raid0 and RAID5 and Simulation of how RAID5 works
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
Little girl walking
Alibaba cloud architects interpret the four mainstream game architectures
MySQL of database -- Fundamentals
ASUS laptop can't read USB and surf the Internet after reinstalling the system
php 二维数组指定元素相等后相加否则新增
Where is int a = 1 stored
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》