【CodeForces - 1153D】Serval and Rooted Tree(树形dp)
題干:
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?vvis 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,…,kto 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?fifirepresents the parent of the node?ii.
Output
Output one integer?— the maximum possible number in the root of the tree.
Examples
Input
6 1 0 1 1 0 1 1 2 2 2 2Output
1Input
5 1 0 1 0 1 1 1 1 1Output
4Input
8 1 0 0 1 0 1 1 0 1 1 2 2 3 3 3Output
4Input
9 1 1 0 0 1 0 1 0 1 1 1 2 2 3 3 4 4Output
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.
題目大意:
? ?n個節(jié)點的一棵樹,1號節(jié)點是根節(jié)點。每個點有屬性0或1,分別代表可以獲得子樹的最小權值? 子樹的最大權值。假設有k個葉子結(jié)點,那么這k個節(jié)點的權值為1~k,自由分配。問你根節(jié)點可以獲得的最大權值是多少?
解題報告:
? ?對于每棵子樹來說,根節(jié)點的值越大越好,因為有操作符的存在,無法準確求出根結(jié)點的值,但可以求出根節(jié)點的值在這個子樹所有葉節(jié)點的值中大小排第幾。這樣設定狀態(tài)比較方便合并子問題。
用dp[i]表示以 i 為根節(jié)點的子樹中,根節(jié)點 i 所得到的在子樹中排名的最大值。
對于每一種情況,我們都從小到大分配權值,這是為了方便簡化思考。
如果屬性值為1,考慮到最終只能傳一個值到根節(jié)點,所以我們枚舉是從第j棵子樹的值傳遞上來的。這樣我們肯定要讓其他葉子結(jié)點都是最小值,所以我們的dp[cur]就是max(sum[cur]-sum[v] + dp[v]);
如果屬性值為0,那么我們先讓權值從小到大均勻分配,每個子節(jié)點分配dp[i]-1個葉子,然后任選一個點當做往上傳的點,這樣dp[cur]=∑(dp[i]-1) + 1;
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 3e5 + 5; int n; int sum[MAX],dp[MAX],val[MAX]; vector<int> vv[MAX]; void dfs(int cur,int rt) {dp[cur]=1;if(vv[cur].size() == 0) {sum[cur] = 1;//葉子結(jié)點的數(shù)量 return ;}int up = vv[cur].size(),tmp=0;for(int i = 0; i<up; i++) {int v = vv[cur][i];dfs(v,cur);sum[cur] += sum[v];}if(val[cur] == 1) {for(int i = 0; i<up; i++) {int v = vv[cur][i];dp[cur] = max(dp[cur],sum[cur]-sum[v]+dp[v]);}}else {for(int i = 0; i<up; i++) {int v = vv[cur][i];tmp += sum[v]-dp[v];}dp[cur] = sum[cur]-tmp-up+1;} } int main() {cin>>n;for(int i = 1; i<=n; i++) scanf("%d",val+i);for(int tmp,i = 2; i<=n; i++) scanf("%d",&tmp),vv[tmp].pb(i);dfs(1,-1);printf("%d\n",dp[1]);return 0 ; }是我太菜了,,去重修dp了、、?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 1153D】Serval and Rooted Tree(树形dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SonicStageMonitoring
- 下一篇: 信用卡申请冻结 冻结不等于挂失别对它置之