题意:

  就是有一颗树  然后每次询问 父结点 的 第k个结点是不是他的子嗣。。。是的话就输出这个子嗣。。不是 就输出-1

解析:

  突然想到后缀数组的sa 和 x的用法。。就是我们可以用一个id标记当前结点的等级 用idx标记等级ans是哪一个结点。。然后用en标记结点u的子嗣的结束结点

  然后每次判断一下就可以了

  

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e6+, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
typedef long long LL;
int vis[maxn], id[maxn], idx[maxn], en[maxn];
vector<int> G[maxn]; int n, q;
int ans;
void dfs(int u)
{
id[ans] = u;
idx[u] = ans;
// cout<< u << " << " " << idx[u] <<endl;
// if(ans == n) return;
for(int i=; i<G[u].size(); i++)
{
int v = G[u][i];
ans++;
dfs(v);
}
en[u] = ans;
} int main()
{
rd(n), rd(q);
int tmp;
ans = ;
for(int i=; i<=n; i++)
{
rd(tmp);
G[tmp].push_back(i);
}
dfs();
int u, k;
// for(int i=1; i<=n; i++)
// cout<< i << " " <<idx[i] << " " << en[i] <<endl; for(int i=; i<q; i++)
{
rd(u), rd(k);
int temp = idx[u];
if(id[temp+k-] == || idx[id[temp+k-]] > en[u])
printf("-1\n");
else
printf("%d\n", id[temp+k-]);
} return ;
}

Military Problem CodeForces - 1006E(dfs搜一下 标记一下)的更多相关文章

  1. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

  2. BZOJ 2435: [Noi2011]道路修建 dfs搜图

    2435: [Noi2011]道路修建 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他 ...

  3. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  4. B - Save the problem! CodeForces - 867B 构造题

    B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...

  5. CodeForces 1006E Military Problem(DFS,树的选择性遍历)

    http://codeforces.com/contest/1006/problem/E 题意: 就是给出n,m,共n个点[1,n],m次询问.第二行给出n-1个数a[i],2<=i<=n ...

  6. Codeforces Round #498 (Div. 3) E. Military Problem (DFS)

    题意:建一颗以\(1\)为根结点的树,询问\(q\)次,每次询问一个结点,问该结点的第\(k\)个子结点,如果不存在则输出\(-1\). 题解:该题数据范围较大,需要采用dfs预处理的方法,我们从结点 ...

  7. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

  8. Codeforces Round #498 (Div. 3)--E. Military Problem

    题意问,这个点的然后求子树的第i个节点. 这道题是个非常明显的DFS序: 我们只需要记录DFS的入DFS的时间,以及出DFS的时间,也就是DFS序, 然后判断第i个子树是否在这个节点的时间段之间. 最 ...

  9. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

随机推荐

  1. MIPI Alliance (MIPI联盟)

    一.介绍 1.MIPI联盟,即移动产业处理器接口(Mobile Industry Processor Interface 简称MIPI)联盟.MIPI(移动产业处理器接口)是MIPI联盟发起的为移动应 ...

  2. MSTECHLNK

    MSTECHLNK(微软技术直通车) 时间:2017.12.16地点:微软中关村办公楼天安门会议室

  3. 20155313 杨瀚 《网络对抗技术》实验一 PC平台逆向破解(5)M

    exp1 PC平台逆向破解(5)M 一.实验内容 1.手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 2.利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发 ...

  4. PYQT5实现控制台显示功能

    首先,写一个信号,用来发射标准输出作为信号 class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) #定义 ...

  5. POJ3278&&1426&&3126&&3087&&3414

    接上次的,标签是BFS专题 其实无论是Deepth还是Breadth都是Search 3278 又是撸过的题目 1426 找一个十进制数(我刚开始看样例以为是二进制数),使得它每一位上都是1或0,且是 ...

  6. MFC 用ShellExecute打开外部文件

    知识点: 获取CListCtrl选中文本 用ShellExecute打开外部文件 一.CListCtrl::GetFirstSelectedItemPosition CListCtrl::GetFir ...

  7. H5——video百花齐放(浏览器自带的播放器)

    前言 手机自带浏览器的H5播放器 真是百花齐放啊(各个手机厂家有各个厂家的控件UI) 需求 手机浏览器木有控件条 自动播放 全屏处理 监控进度条 快进后退 自动播放 自动播放就给跪了 ios 安卓 为 ...

  8. 浅谈iOS 自动调节文本高度

    文字展示是任何GUI开发的一个最常规的编程任务.可能一提及文字我们脑中想到的无非就是 Label 和 Text 这两个关键词,今天我们就谈谈 Label. 无论在 Windows 或者 Web 开发中 ...

  9. js的各种正则表达式

    验证各种手机包括成都"028-"开头的座机号验证 if (!(/^(16[8]|13[0-9]|15[0|3|6|7|8|9]|18[7])\d{8}|(028-)\d{7}$/. ...

  10. Configuration Section Designer for VS2017

    Configuration Section Designer是在Visual Studio中设计符合.Net配置体系配置文件和代码的神器.然而,它的源码已经很久不维护了.现在在新的VS2017中无法使 ...