思维题--code forces round# 551 div.2

时间:2022-09-17 14:19:23

思维题--code forces round# 551 div.2-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 11represents 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 2

output

Copy

1

input

Copy

5
1 0 1 0 1
1 1 1 1

output

Copy

4

input

Copy

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

output

Copy

4

input

Copy

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

output

Copy

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.

思维题--code forces round# 551 div.2

In the second example, no matter how you arrange the numbers, the answer is 44.

思维题--code forces round# 551 div.2

In the third example, one of the best solution to achieve 44 is to arrange 44 and 55 to nodes 44 and 55.

思维题--code forces round# 551 div.2

In the fourth example, the best solution is to arrange 55 to node 55.

思维题--code forces round# 551 div.2

题意易懂

思路

让所有叶子的值都为1

算的是必要的叶子个数,那么答案就是叶子个数 - 必要的叶子个数 + 1

如果是取max,那么该节点要取最大,那必要的叶子个数取决于该节点的所有子节点中,最小的必要叶子个数

如果是取min,那么该节点要取最大,必要的叶子个数为该节点的所有子节点中,所有的必要叶子个数的和

因为题目输入格式,可以知道父节点的输入一定在子节点前面,所以能遍历

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 300000+50;
const int MOD = 1e9 + 9; #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i) vector<int> v[N];
int a[N], c[N];
int n; int main()
{
cin >> n;
F(i, 1, n) cin >> a[i];
F(i, 2, n)
{
int t;
cin >> t;
v[t].push_back(i);
} int cnt = 0, ans = 0;
RF(i, n, 1)
{
if(v[i].size() == 0)
{
c[i] = 1;
cnt++;
}
else if(a[i])
{
c[i] = INF;
F(j, 0, v[i].size() - 1)
c[i] = min(c[i], c[v[i][j]]);
}
else
{
F(j, 0, v[i].size() - 1)
c[i] += c[v[i][j]];
}
}
cout << cnt + 1 - c[1] << endl;
return 0;
}

dfs写法

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 300000+50;
const int MOD = 1e9 + 9; #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i) vector<int> v[N];
int a[N], c[N];
int n, ant = 0, cnt; void dfs(int now)
{
if(v[now].size() == 0)
{
c[now] = 1;
cnt++;
}
else if(a[now])
{
c[now] = INF;
F(j, 0, v[now].size() - 1)
{
dfs(v[now][j]);
c[now] = min(c[now], c[v[now][j]]);
}
}
else
{
F(j, 0, v[now].size() - 1)
{
dfs(v[now][j]);
c[now] += c[v[now][j]];
}
}
} int main()
{
cin >> n;
F(i, 1, n) cin >> a[i];
F(i, 2, n)
{
int t;
cin >> t;
v[t].push_back(i);
}
dfs(1);
cout << cnt + 1 - c[1] << endl;
return 0;
}

思维题--code forces round# 551 div.2的更多相关文章

  1. 【Codeforces】Codeforces Round &num;551 &lpar;Div&period; 2&rpar;

    Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...

  2. CF Round &num;551 &lpar;Div&period; 2&rpar; D

    CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...

  3. 水题 Codeforces Beta Round &num;70 &lpar;Div&period; 2&rpar; A&period; Haiku

    题目传送门 /* 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 下午网速巨慢:( */ #include <cstdio> #include <cstring> ...

  4. Codeforces Round &num;551 &lpar;Div&period; 2&rpar; E&period; Serval and Snake &lpar;交互题&rpar;

    人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...

  5. Codeforces Round &num;551 &lpar;Div&period; 2&rpar; 题解

    CF1153A 直接做啊,分类讨论即可 #include<iostream> #include<string.h> #include<string> #includ ...

  6. C&period; Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round &num;551 &lpar;Div&period; 2&rpar;

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  7. 【做题】Codeforces Round &num;429 &lpar;Div&period; 2&rpar; E&period; On the Bench——组合问题&plus;dp

    题目大意是给你n个数,求相邻两数相乘不是完全平方数的排列数. 一开始看到这题的时候,本人便想给相乘为完全平方数的数对建边,然后就写萎了... 后来通过集体智慧发现这个重要性质:对于自然数a,b,c,若 ...

  8. Codeforces Round &num;551 &lpar;Div&period; 2&rpar; A~E题解

    突然发现上一场没有写,那就补补吧 本来这场应该5题的,结果一念之差E fail了 A. Serval and Bus 基本数学不解释,假如你没有+1 -1真的不好意思见人了 #include<c ...

  9. 【思维题】TCO14 Round 2C InverseRMQ

    全网好像就只有劼和manchery写了博客的样子……:正解可能是最大流?但是仔细特判也能过 题目描述 RMQ问题即区间最值问题是一个有趣的问题. 在这个问题中,对于一个长度为 n 的排列,query( ...

随机推荐

  1. wordcount 过程

    hdfs原始数据 hello a hello b map阶段: 输入数据:<0,"hello a"> <8,"hello b"> key ...

  2. 字符集与编码01--charset vs encoding

    声明:此文章转载自 http://my.oschina.net/goldenshaw/blog/304493 许多时候,字符集与编码这两个概念常被混为一谈,但两者是有差别的,作为深入理解的第一步,首先 ...

  3. Java集合之TreeMap

    Map的单元是对键值对的处理,之前分析过的两种Map,HashMap和LinkedHashMap都是用哈希值去寻找我们想要的键值对,优点是由O(1)的查找速度. 那如果我们在一个对查找性能要求不那么高 ...

  4. struts2讲义----二

    Struts的namespace 示例工程Struts2_0200_Namespace Struts.xml <struts> <constant name="struts ...

  5. MSG 结构

    MSG 消息结构 在 Windows 程序中,消息是由 MSG 结构体来表示的. 结构原型: typedef struct tagMSG { HWND   hwnd; UINT   message; ...

  6. 一个相对健壮的node 静态http服务器

    先上代码,然后说说坑,算是一个总结,以后在mac上就用这个开启服务调试了,挺好.然后接着想写一个动态的返回页面的,刚好练手mv*的框架. var http = require('http'); var ...

  7. &lbrack;CF833B&rsqb; The Bakery

    Description 将一个长度为n的序列分为k段 使得总价值最大一段区间的价值表示为区间内不同数字的个数 \(n\leq 35000,k\leq 50,1\leq a_i\leq n\) Solu ...

  8. idea 高级调试技巧

    两年前写过一篇关于idea的高级用法,今天再来一篇关于调试方面的技巧讲解: 一.条件断点 循环中经常用到这个技巧,比如:遍历1个大List的过程中,想让断点停在某个特定值. 参考上图,在断点的位置,右 ...

  9. Django Model&period;&lowbar;meta API

    Model._meta API是Django ORM的核心,它使得lookups.queries.forms.admin这些模块通过每个model类的_meta的属性可以了解每个model的情况. 1 ...

  10. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...