原题链接:http://codeforces.com/problemset/problem/1153/D
题目原文:
D. Serval and Rooted Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 vv is the last different from vv vertex on the path from the root to the vertex vv. Children of vertex vv are all nodes for which vv is the parent. A vertex is a leaf if it has no children.
The rooted tree Serval owns has nn nodes, node 11 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 kk leaves in the tree. Serval wants to put integers 1,2,…,k1,2,…,k to the kk 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?
Input
The first line contains an integer nn (2≤n≤3⋅1052≤n≤3⋅105), the size of the tree.
The second line contains nn integers, the ii-th of them represents the operation in the node ii. 00 represents minmin and 11 represents maxmax. If the node is a leaf, there is still a number of 00 or 11, but you can ignore it.
The third line contains n−1n−1 integers f2,f3,…,fnf2,f3,…,fn (1≤fi≤i−11≤fi≤i−1), where fifi represents the parent of the node ii.
Output
Output one integer — the maximum possible number in the root of the tree.
Examples
input
Copy
6 1 0 1 1 0 1 1 2 2 2 2output
Copy
1input
Copy
5 1 0 1 0 1 1 1 1 1output
Copy
4input
Copy
8 1 0 0 1 0 1 1 0 1 1 2 2 3 3 3output
Copy
4input
Copy
9 1 1 0 0 1 0 1 0 1 1 1 2 2 3 3 4 4output
Copy
5Note
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 11.
In the second example, no matter how you arrange the numbers, the answer is 44.
In the third example, one of the best solution to achieve 44 is to arrange 44 and 55 to nodes 44 and 55.
In the fourth example, the best solution is to arrange 55 to node 55.
题目大意:
给一棵树,树有min/max标记,设叶子节点数量为k,则让你分别给每个叶子节点赋[1, k]值 ,通过max/min计算后,使得根1节点的值最大。
解题思路:
权值是自己任意分配,观察可以发现,有些节点对根节点的值是没有贡献的,那么可以把问题看成对根节点有贡献的叶子节点个数。然后我们可以把[1, k]从大到小分配给这些叶子节点。贪心的想:如果根是max,那么肯定选择子树中叶子节点最少的;如果根是min,那么所有子树都要考虑了。dp[i] 为以 i 为根的子树叶子节点个数,如果 i 为 max,dp[i] = min { dp[i], dp[child[i][j]] }, 如果 i 为 min,dp[i] += dp[i][child[i][j]]; 答案为叶子节点总个数 all - dp[1] + 1.
AC代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = int(3e5 + 5);
const int INF = 0x3f3f3f3f;
int n;
int o[N]; // operation
vector<int> tree[N];
int dp[N];
int all;
void dfs(int r)
{
if (tree[r].empty())
{
dp[r] = 1;
all++;
return;
}
if (o[r] == 1) dp[r] = INF;
else dp[r] = 0;
int i;
for (i = 0; i < tree[r].size(); i++)
{
dfs(tree[r][i]);
if (o[r] == 1) dp[r] = min(dp[r], dp[tree[r][i]]);
else dp[r] += dp[tree[r][i]];
}
}
int main()
{
int i;
while (scanf("%d", &n) != EOF)
{
all = 0;
for (i = 1; i <= n; i++)
{
tree[i].clear();
scanf("%d", &o[i]);
}
int f;
for (i = 2; i <= n; i++)
{
scanf("%d", &f);
tree[f].push_back(i);
}
dfs(1);
printf("%d\n", all - dp[1] + 1);
}
return 0;
}