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;
}

 

 

时间: 2024-07-30 14:19:00

cf 151.div2 E Blood Cousins Return的相关文章

cf 151.div2 C - Beauty Pageant

C - Beauty Pageant         很简单的一道题,就是给出k种不同的组合就可以了,比赛的时候没有注意到k的范围,直接dfs,果断超时,k只有(n+1)n/2种,所以可以直接枚举即可--       以后要看好范围再写     dfs代码(修改后) /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */

cf 167.div2 D.Dima and Two Sequences

    今天的cf完全不在状态,A题10分钟才读懂题意.目测要掉rating了     这题题意很简单,就是按X升序排列会有多少种情况,一个排列组合的问题     设相同x的元素个数为Xi  ,完全相同为2K(易知若完全相同有且仅有2个,比赛的时候就没想到这一点)     ans= ∑Xi!/2^k    之所以能在阶乘中除2^k,是因为k最大为n/2,而n!中的2的因子至少为n/2 /* author:jxy lang:C/C++ university:China,Xidian Univers

cf 153.div2 D. Playing with Permutations

D. Playing with Permutations        比赛的时候一直没看懂题,后来看了下,其实很简单,就是给你一个变换序列q ,有两种变换1和2,1就是令Pi=Pqi,2则相反,互为逆变换,所以只要单独求出每种变换要多少少次,即可判断出来能否成功变换.   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source*

cf 153.div2 B. Unsorting Array

  B. Unsorting Array         最近写题目越来越少了,以至于这种简单题都会错--比赛的时候写了个n^2的算法,果断超时,赛后加了个剪枝,78ms         但是其实因为只要把有序序列中的连续不相等的两项交换,即可使之无序--所以o(n)就够了   o(n^2) /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate th

cf 204 div2 C Jeff and Rounding 模拟

    智商题,如果没有0就很简单,一半加一半减,恒定的,和选择无关.有0的话就可以选择和某些配对,于是就可以更改加减次数.而枚举加减次数即可,比赛时就没想清楚这一点.具体见代码 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio>

CF 203 div2 E. Wrong Floyd 图论

    题目中只选取k个点更新,因此只要保证有一个点只连到非k点即可     注意:题目要求连通图!!比赛的时候没看到,WA了,只要改下输出顺序即可保证联通. /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include

cf 154.div2 D. Table with Letters - 2

D. Table with Letters - 2         今天的比赛题比较奸诈,居然是文件读入的,让A题耽误了很多时间.           这题是dp的一个经典类型,o((n*m)^2)的算法非常好写,但是必然TLE.因为其要求四角均相等,不妨以此为条件进行判断,即可缩为o(n^2*m)算法   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please ind

cf 167.div2 E.Dima and Horses

    题意很简单,就是分组,让冲突最多有一种,因为只要分成2组,就是个2-SAT     但是最多只有3个不和,就是不和的三个里至少有2个在一组,所以应该必然有解(这边不太明白,感觉这样),直接染色就可以了 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #includ

cf 156.div2 C Almost Arithmetical Progression

     貌似是两周前的比赛,一直忙考试没写.      C题就是找个最长的交替序列,直接模拟可以过,比赛的时候以为会超时就没写,刚写了下居然过了      也可以用dp,dp[i][j]=dp[j][last]+1,last为最后一个和i相等的数下标,就是i和last是两个相同的数,中间加一个j为任意数   模拟代码: /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,ple