[树形dp]CCC-Unfriend

题面

题面描述

Mark invited some people to join his social network. Some of them invited new people, who invited new people, and so on. Now there are N N N people in the network, numbered from 1 1 1 to N N N.
Mark has decided to remove some people and keep others. There is one restriction: when removing a person, he will also remove the people s/he invited, and the people they invited, and so on. Mark will never remove himself, and we do not allow people to be invited by more than one person.
Mark can also decide to not remove anyone.
How many different sets of people can be removed?

翻译

马克邀请了一些朋友参加舞会。 其中一些朋友又邀请了新的朋友,这些被邀请的朋友又可能继续邀请其它新朋友。在这样的邀请后,现在舞会总共可能有 N N N个人,从 1 1 1 N N N编号。
马克觉得人数太多了,可能会拒绝一些被邀请的人。当然,一旦某个人被拒绝参加,则他邀请的朋友自然也不会参加了,同样的,被拒绝的人邀请的朋友们继续邀请的人也不会参加了。当然马克也有可能不拒绝任何人。
现在马克想知道共有多少种不同的拒绝方案。

输入

The first line contains a single integer N N N ( N ≤ 6 N ≤ 6 N6), the number of people in the network. Next are N − 1 N - 1 N1 lines telling us who invited each person. To be precise, line i i i in this set ( 1 ≤ i ≤ N − 1 1 ≤ i ≤ N - 1 1iN1) contains a single integer j j j (with j > i j > i j>i), which indicates that person j j j is the person who invited person i i i. Person N N N is Mark.

翻译

第一行是 N N N,表示舞会共有 N N N个人参加 ( N < = 6 ) (N<=6) N<=6,其中马克的编号为 N N N
第二行开始表示邀请的情况,其中第 i i i行有一个数字 j j j,表示 j j j号朋友邀请了 i − 1 i-1 i1号朋友
数据保证所有人都被马克直接或间接地邀请,每个人可能邀请多个人,但是每个人只会被 1 1 1个人邀请。

输出

Output a single integer, the number of possible sets of people that can be removed.

翻译

输出可能的方案数。

样例

in1:

3
3
3

out1:

4

in2:

4
3
4
4

out2:

6

提示

Explanation for Sample 1

The first number of the input indicates there are three people in the network. The next line tells
us that Person 1 1 1 was invited by Mark, while the last line tells us that Person 2 2 2 was also invited by
Mark. The sets of people that can be removed are { } \{\} {}, { 1 } \{1\} {1}, { 2 } \{2\} {2}, { 1 , 2 } \{1,2\} {1,2}.
Sample Input 2
4 3 4 4
Output for Sample Input 2
6

Explanation for Sample 2

are not possible, since when you remove 3, you must also remove 1.
There are 4 people in the network. Here is a table of who invited who:

Personinviting Invited
1none
2none
31
42,3

The possible sets are { } \{\} {}, { 1 } \{1\} {1}, { 2 } \{2\} {2}, { 1 , 2 } \{1,2\} {1,2}, { 1 , 3 } \{1,3\} {1,3}, and { 1 , 2 , 3 } \{1,2,3\} {1,2,3}. Notice that the sets { 3 } \{3\} {3} and { 2 , 3 } \{2,3\} {2,3}
are not possible, since when you remove 3 3 3, you must also remove 1 1 1.

翻译

样例1

输入的第一个数字表示网络中有三个人。下一行说明
我们说,这个人 1 1 1是马克邀请的,而最后一行告诉我们 2 2 2的人也是被邀请的
作记号。可以删除的用户集是 { } \{\} {} { 1 } \{1\} {1} { 2 } \{2\} {2} { 1 , 2 } \{1,2\} {1,2}
样本输入2
4 3 4 4
样本输入2的输出
6

样例2

不可能,因为当您删除3时,还必须删除1。
网络里有4个人。以下是谁邀请了谁:

被邀请人
1
2
31
42,3

可能的集合有 { } \{\} {} { 1 } \{1\} {1} { 2 } \{2\} {2} { 1 , 2 } \{1,2\} {1,2} { 1 , 3 } \{1,3\} {1,3} { 1 , 2 , 3 } \{1,2,3\} {1,2,3}。注意集合 { 3 } \{3\} {3} { 2 , 3 } \{2,3\} {2,3}
不可能,因为删除 3 3 3时,还必须删除 1 1 1

算法分析

树形

因为每个人只能被一个人邀请,所以显然是一个树形结构的题
样例1
样例1
样例2
样例2

dp

因为每个节点的答案都可以从他的儿子们推出来,显然树形dp
拿一组大的样例:
6 6 5 4 5 6

手推一下每个点的情况:

可以看出:
在这里插入图片描述

int dfs(int n){
	int ans=1;
	for(int i=1;i<=N;i++){
		if(f[n][i]==1){
			ans*=dfs(i)+1;
		}
	}
	return ans;
}

完整代码

#include<bits/stdc++.h>
using namespace std;
int N;
int f[139][139];
int dfs(int n){
	int ans=1;
	for(int i=1;i<=N;i++){
		if(f[n][i]==1){
			ans*=dfs(i)+1;
		}
	}
	return ans;
}
int main() {
	scanf("%d",&N);
	int x,y;
	for(int i=1;i<N;i++){
		scanf("%d",&x);
		f[x][i]=1;
	}
	printf("%d\n",dfs(N));
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值