cf 151.div2 E Blood Cousins Return

简介:


Blood Cousins Return

 

      比赛的时候用的裸的dfs,直接超时。其实只要加个记录,记录下每一层的节点,然后每次询问时再查询就好了。

 

附上网上的代码

#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
using namespace std;

const int maxn=200020;
vector<int> v[maxn];
vector< pair<int,int> > point[maxn];
map< pair<int,int>,int > mp;
int n,m,w,k,pppp=0,depth[maxn],pos[maxn][2];
char s[maxn][33];

void Dfs(int k,int d) {
	pos[k][0]=++pppp;
	point[d].push_back(make_pair(pppp,k));
	depth[k]=d;
	for (vector<int>::iterator p=v[k].begin();p!=v[k].end();++p) Dfs(*p,d+1);
	pos[k][1]=++pppp;
}

int Work() {
	if (mp[make_pair(w,k)]) return mp[make_pair(w,k)]-1;
	set<string> list;
	vector< pair<int,int> >::iterator
		l=lower_bound(point[depth[w]+k].begin(),point[depth[w]+k].end(),make_pair(pos[w][0],maxn)),
		r=lower_bound(point[depth[w]+k].begin(),point[depth[w]+k].end(),make_pair(pos[w][1],0));
	for (vector< pair<int,int> >::iterator p=l;p!=r;++p) list.insert(s[(*p).second]);
	return (mp[make_pair(w,k)]=list.size()+1)-1;
}

int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;++i) {scanf("%s%d",&s[i],&k);v[k].push_back(i);}
	Dfs(0,0);
	scanf("%d",&m);
	for (int i=1;i<=m;++i) {scanf("%d%d",&w,&k);printf("%d\n",Work());}
	return 0;
}


 

 

目录
相关文章
|
9天前
|
数据采集 人工智能
这就是我为什么推荐使用var aa = for (var i = 0, l = aa.length; i < l; i++) {var a = aa[i];}循环的原因,每秒最快可以执行4000+次!
这就是我为什么推荐使用var aa = for (var i = 0, l = aa.length; i < l; i++) {var a = aa[i];}循环的原因,每秒最快可以执行4000+次!
|
3月前
汇编指令学习(ADD,SUB,MUL,DIV,XADD,INC,DEC,NEG)
汇编指令学习(ADD,SUB,MUL,DIV,XADD,INC,DEC,NEG)
24 0
|
6月前
|
人工智能 供应链
REF615 HCFFA EAGAN B2B AA 1XD
REF615 HCFFA EAGAN B2B AA 1XD
32 0
|
7月前
|
机器学习/深度学习 人工智能
B2. Wonderful Coloring - 2<734.div3>
B2. Wonderful Coloring - 2<734.div3>
31 1
|
12月前
|
存储
CVI2012报错:Function imaqReadFile: (errorCode == -1074396120 [0xbff60428]). Not an image.问题解决
CVI2012报错:Function imaqReadFile: (errorCode == -1074396120 [0xbff60428]). Not an image.问题解决
|
Linux
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
147 0
组装数据- 对象里面是key:value, value里面是数组的形式,如 {key:[aa,bb], key:[cc,dd]}
组装数据- 对象里面是key:value, value里面是数组的形式,如 {key:[aa,bb], key:[cc,dd]}
组装数据- 对象里面是key:value, value里面是数组的形式,如 {key:[aa,bb], key:[cc,dd]}
How to find unit test class by code
Created by Wang, Jerry, last modified on Dec 20, 2014
101 0
How to find unit test class by code