日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

發布時間:2023/12/10 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 2

Output

1

Input

5 1 0 1 0 1 1 1 1 1

Output

4

Input

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

Output

4

Input

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

Output

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?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個節點的一棵樹,1號節點是根節點。每個點有屬性0或1,分別代表可以獲得子樹的最小權值? 子樹的最大權值。假設有k個葉子結點,那么這k個節點的權值為1~k,自由分配。問你根節點可以獲得的最大權值是多少?

解題報告:

? ?對于每棵子樹來說,根節點的值越大越好,因為有操作符的存在,無法準確求出根結點的值,但可以求出根節點的值在這個子樹所有葉節點的值中大小排第幾。這樣設定狀態比較方便合并子問題。

用dp[i]表示以 i 為根節點的子樹中,根節點 i 所得到的在子樹中排名的最大值。

對于每一種情況,我們都從小到大分配權值,這是為了方便簡化思考。

如果屬性值為1,考慮到最終只能傳一個值到根節點,所以我們枚舉是從第j棵子樹的值傳遞上來的。這樣我們肯定要讓其他葉子結點都是最小值,所以我們的dp[cur]就是max(sum[cur]-sum[v] + dp[v]);

如果屬性值為0,那么我們先讓權值從小到大均勻分配,每個子節點分配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;//葉子結點的數量 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了、、?

總結

以上是生活随笔為你收集整理的【CodeForces - 1153D】Serval and Rooted Tree(树形dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。