Codeforces 246E Blood Cousins Return 树上启发式合并

E. Blood Cousins Return
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus got hold of a family tree. The found tree describes the family relations of n people, numbered from 1 to n. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique.

We call the man with a number a a 1-ancestor of the man with a number b, if the man with a number a is a direct ancestor of the man with a number b.

We call the man with a number a a k-ancestor (k > 1) of the man with a number b, if the man with a number b has a 1-ancestor, and the man with a number a is a (k - 1)-ancestor of the 1-ancestor of the man with a number b.

In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an x-ancestor of himself, for some xx > 0).

We call a man with a number a the k-son of the man with a number b, if the man with a number b is a k-ancestor of the man with a number a.

Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote m pairs of numbers viki. Help him to learn for each pair viki the number of distinct names among all names of the ki-sons of the man with number vi.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 105) — the number of people in the tree. Next n lines contain the description of people in the tree. The i-th line contains space-separated string siand integer ri (0 ≤ ri ≤ n), where si is the name of the man with a number i, and ri is either the number of the direct ancestor of the man with a number i or 0, if the man with a number i has no direct ancestor.

The next line contains a single integer m (1 ≤ m ≤ 105) — the number of Polycarpus's records. Next mlines contain space-separated pairs of integers. The i-th line contains integers viki (1 ≤ vi, ki ≤ n).

It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters.

Output

Print m whitespace-separated integers — the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input.

Examples
input
6
pasha 0
gerald 1
gerald 1
valera 2
igor 3
olesya 1
5
1 1
1 2
1 3
3 1
6 1
output
2
2
0
1
0
input
6
valera 0
valera 1
valera 1
gerald 0
valera 4
kolya 4
7
1 1
1 2
2 1
2 2
4 1
5 1
6 1
output
1
0
0
0
2
0
0


在一棵树上,每个点都被染色。多次查询某个点Vi子树上距离为Ki的点中,有多少个不同的颜色。


子树上的查询问题,很显然可以用dsu on tree.

对树的每一层开一个map或者set,更新每层的颜色情况。


有一个坑点,查询的层数可能大于树的层数,此时需要特判,以防止下标越界。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int c[maxn],cnt[maxn],siz[maxn],son[maxn],head[maxn],ans[maxn],dep[maxn];
bool visit[maxn],big[maxn];
unordered_map<string,int> mp;
map<int, int> mp2[maxn];
ll sum,mx,num;
int n,m;

struct query{
	int id,d;
	query(int id,int d): id(id),d(d) {}
};
vector<query> q[maxn];

struct Edge {
	int from,to,pre;
};
Edge edge[maxn*2];

void addedge(int from, int to) {
	edge[num].from = from; edge[num].to = to;
	edge[num].pre = head[from]; head[from] = num++;
}

int dfs2(int now,int step) {  
	visit[now] = 1; son[now] = -1; siz[now] = 1; dep[now] = step;
    for (int i=head[now];i!=-1;i=edge[i].pre) {  
        int to=edge[i].to;  
        if (!visit[to]) {  
            siz[now]+=dfs2(to,step+1);  
            if (son[now]==-1||siz[to]>siz[son[now]]) son[now]=to;  
        }  
    }
    if (son[now]!=-1) big[son[now]]=1;
    return siz[now];
}  

void add(int now, int val) {
	int w;
	w = mp2[dep[now]][c[now]] += val;
	if (w == 0&&val==-1) cnt[dep[now]]--; 
	if (w == 1&&val==1) cnt[dep[now]]++;
	for (int i = head[now]; i != -1; i = edge[i].pre) {
		int to = edge[i].to;
		if (!big[to]) add(to, val);
	}
}

void dfs(int now,int fa,bool rem) {
	int i;
	for (i = head[now]; i != -1; i = edge[i].pre) {
		int to = edge[i].to;
		if (to != fa && !big[to]) dfs(to, now, 0);
	}
	if (son[now] != -1) dfs(son[now], now, 1);
	add(now, 1);
	for (i = 0; i < q[now].size(); i++) {
		if (q[now][i].d + dep[now]<=1e5) ans[q[now][i].id] = cnt[q[now][i].d+dep[now]];
		else ans[q[now][i].id] = 0;
	}
	if (son[now] != -1) big[son[now]] = 0;
	if (!rem) add(now, -1);
}

int main() {
	num=0;memset(head,-1,sizeof(head));
	int n,i,j,m=0,x,y,qn;
	string s;
	scanf("%d",&n);
	for (i=1;i<=n;i++) {
		cin >> s >> x;
		if (!mp[s]) c[i]=mp[s]=++m; else c[i]=mp[s];
		addedge(x,i);
	}
	scanf("%d",&qn);
	for (i=1;i<=qn;i++) {
		scanf("%d%d",&x,&y);
		q[x].push_back(query(i,y));
	} 
	mem0(visit);mem0(big);
	dfs2(0,0);
	mem0(cnt);
	dfs(0, -1, 0);
	for (i = 1; i <= qn; i++) printf("%d\n", ans[i]);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值