本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:codeforces246E

正解:$dsu$ $on$ $tree$

解题报告:

  又是套路题…

  用一个$map$存一下每个深度的每种字符串出现次数,另外开一个数组记录一下每个深度的$ans$,然后离线做。

  

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 200011;//数组开小了...
const int MAXM = 400011;
int n,m,father[MAXN],ecnt,first[MAXN],next[MAXM],to[MAXM],size[MAXN],son[MAXN];
int ans[MAXN],Son,deep[MAXN],sum[MAXN];
string ch[MAXN];
struct node{ int id,x; }tmp;
vector<node>w[MAXN];
map<string,int>cnt[MAXN]; inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x){
size[x]=1;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; deep[v]=deep[x]+1;
dfs(v); size[x]+=size[v];
if(size[v]>size[son[x]]) son[x]=v;
}
} inline void add(int x,int type){
if(type==1) {
cnt[ deep[x] ][ ch[x] ]++;
if(cnt[ deep[x] ][ ch[x] ]==1) sum[ deep[x] ]++;
}
else {
cnt[ deep[x] ][ ch[x] ]--;
if(cnt[ deep[x] ][ ch[x] ]==0) sum[ deep[x] ]--;
}
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==Son) continue;
add(v,type);
}
} inline void solve(int x,bool top){
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==son[x]) continue;
solve(v,1);
} if(son[x])
solve(son[x],0),Son=son[x]; add(x,1);
Son=0; for(int i=0,ss=w[x].size();i<ss;i++) {
tmp=w[x][i];
ans[tmp.id]=sum[deep[x]+tmp.x/*!!!*/];
} if(top)
add(x,-1);
} inline void work(){
n=getint();
for(int i=1;i<=n;i++) {
cin>>ch[i];
father[i]=getint();
link(father[i],i);
}
m=getint(); int x,y;
for(int i=1;i<=m;i++) { x=getint(); y=getint(); tmp.id=i; tmp.x=y; w[x].push_back(tmp); }
for(int i=1;i<=n;i++)
if(father[i]==0)
dfs(i);
for(int i=1;i<=n;i++)
if(father[i]==0)
solve(i,1); for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

最新文章

  1. easyui的datagrid form(表单)提交到后台转对象的时候中文出现乱码
  2. IntelliJ IDEA 绝对好用快捷键
  3. ${param.xxx}获取url中的参数
  4. 31、springmvc(注解)
  5. React Native中的网络请求fetch和简单封装
  6. struts2启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
  7. thinkphp中的where()方法
  8. OpenWRT编译记录--TPLINK_WR841ND_V7
  9. 201521123037 《Java程序设计》第14周学习总结
  10. popup方法
  11. Java基础系列--static关键字
  12. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
  13. 十分钟释疑Oracle中“小表超慢”之谜(SQL调优/SQL优化)
  14. ReactiveSwift源码解析(六) SignalProtocol的take(first)与collect()延展实现
  15. ubuntu部署php7.1
  16. [Android] JNI中的Local Reference
  17. day27(反射之内省机制)
  18. VS2012 生成项目报 &quot;Lc.exe已退出,代码为-1&quot; 错误
  19. JXL读取,写入Excel
  20. 微信小程序 - wxpage

热门文章

  1. Spark高速入门指南(Quick Start Spark)
  2. DotNet Core全新认识
  3. Powerdesigner显示列名
  4. flatpickr功能强大的日期时间选择器插件
  5. PHP魔术方法和魔术常量介绍及使用
  6. Apache Spark 2.0三种API的传说:RDD、DataFrame和Dataset
  7. 创建正真的Java不可变类
  8. 安卓和ios的区别
  9. ORM到底是用还是不用?(复制)
  10. vs git 推送远程会失败.