Codeforces Round #551 (Div. 2) Serval and Rooted Tree

Serval and Rooted Tree

传送门

题目描述:

Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before.
As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all nodes for which v is the parent. A vertex is a leaf if it has no children.
The rooted tree Serval owns has n nodes, node 1 is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation maxmax or minmin written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively.
Assume that there are k leaves in the tree. Serval wants to put integers 1,2,…,k to the k leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him?

输入:

The first line contains an integer n (2 ≤ \leq n ≤ \leq 3* 1 0 5 10^5 105), the size of the tree.The second line contains n integers, the i-th of them represents the operation in the node i. 0 represents minmin and 1 represents maxmax. If the node is a leaf, there is still a number of 0 or 1, but you can ignore it.The third line contains n−1 integers f2,f3,…,fn(1 ≤ \leq fi ≤ \leq i−1), where fi represents the parent of the node i.

输出:

Output one integer — the maximum possible number in the root of the tree.

输入样例:

6
1 0 1 1 0 1
1 2 2 2 2

5
1 0 1 0 1
1 1 1 1

8
1 0 0 1 0 1 1 0
1 1 2 2 3 3 3

9
1 1 0 0 1 0 1 0 1
1 1 2 2 3 3 4 4

输出样例:

1
4
4
5

Note:

Pictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes. In the first example, no matter how you arrange the numbers, the answer is 1
在这里插入图片描述
In the second example, no matter how you arrange the numbers, the answer is 4.
在这里插入图片描述

In the third example, one of the best solution to achieve 4 is to arrange 4 and 5 to nodes 4 and 5.
在这里插入图片描述
In the fourth example, the best solution is to arrange 5 to node 5.
在这里插入图片描述

题意:

给定一棵树,对于一个节点,其有两种属性值,若为0,则从子树中找一个值最小的,若为1,则从子树中找一个值最大的。对于这棵树,若有k个叶子结点,则把1~k分别填入叶子结点,作为结点的值。输出根节点可能取到的最大值。

思路:

开始时并不知道根节点的值,但通过叶子结点可以推到根节点,很明显是一道树形DP的题目。
对于一道DP的题目,我们首先要确定DP的含义
在此题中DP[i]=k表示第i个结点它能取到第K大的数
由此写出状态转移方程
对于结点i,其子结点为j则
i结点属性值为0时,dp[i]= ∑ \sum dp[j]
i结点属性值为1时,dp[i]=min(dp[i],dp[j])

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn=300005;
vector<int> S[maxn];//记录父子关系
int n;
int a[maxn];//结点的属性
int dp[maxn];
int num;
void dfs(int x)
{
int minn=0,maxx=-1;
for(int i=0;i<S[x].size();i++)//对子结点处理
{
    dfs(S[x][i]);//去找叶子结点
    if(maxx==-1)
        maxx=dp[S[x][i]];
    else
        maxx=min(maxx,dp[S[x][i]]);
    minn+=dp[S[x][i]];
}
if(S[x].size()==0)//若为叶子结点
{
 dp[x]=1;
 num++;
}
else{
    if(a[x]==0)
        dp[x]=minn;//属性值为0,取最小的
    else
        dp[x]=maxx;//属性值为1,取最大的
}
}
int main()
{
while(scanf("%d",&n)==1)
{
 for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
 int x;
 for(int i=1;i<=n;i++)
    S[i].clear();
 for(int i=2;i<=n;i++)
    {scanf("%d",&x);
     S[x].push_back(i);
    }
    num=0;//记录叶子结点数目
    dfs(1);//根节点标号为1
    cout<<num+1-dp[1]<<endl;//有num个叶子结点,根结点取第dp[i]大的,则其的值为num+1-dp[1]
}
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值