题目地址:CF1153D Serval and Rooted Tree

挺好玩儿也挺考思维的一道题

思路:树形DP+贪心

数组 \(d\) 维护这样一个值:

对于一个节点 \(x\) ,它的值最大可以为以 \(x\) 为根的子树中叶子节点的数值中排名第 \(d_x\) 大的数值

感性的理解就是,假如这个节点下有 \(n\) 个叶子节点,儿这个节点的 \(d\) 值为 \(k\) ,那么这个节点最大可以是 \(n+1-k\) 。

假设现在在节点 \(x\) :

如果 \(x\) 是叶子节点,那么 \(d_x = 1\) ;

如果 \(x\) 是 \(min\) 节点,那么 \(d_x = \sum_{y \in son_x}\ d_y\) ;

如果 \(x\) 是 \(max\) 节点,那么 \(d_x = min_{y \in son_x}\ d_y\) 。

那么显然 \(ans = cnt + 1 - d_1\) ,其中 \(cnt\) 为叶子节点个数。

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 6;
int n, a[N], d[N], cnt;
vector<int> e[N];

void dfs(int x) {
    if (!e[x].size()) {
        d[x] = 1;
        ++cnt;
        return;
    }
    d[x] = a[x] ? n : 0;
    for (unsigned int i = 0; i < e[x].size(); i++) {
        int y = e[x][i];
        dfs(y);
        if (a[x]) d[x] = min(d[x], d[y]);
        else d[x] += d[y];
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 2, f; i <= n; i++) {
        scanf("%d", &f);
        e[f].push_back(i);
    }
    dfs(1);
    cout << cnt + 1 - d[1] << endl;
    return 0;
}

CF1153D Serval and Rooted Tree的更多相关文章

  1. CF1153D Serval and Rooted Tree(树形DP)

      题目链接: https://www.luogu.org/problemnew/show/CF1153D (cf崩了,贴了个落谷的) 题目大意:给你n个点,然后n-1条边,构成一棵树,每个点是子节点 ...

  2. D. Serval and Rooted Tree (樹狀DP)

    Codeforce 1153D Serval and Rooted Tree (樹狀DP) 今天我們來看看CF1153D 題目連結 題目 給一棵數,假設有$k$個葉節點,我們可以給葉節點分配$1$~$ ...

  3. Codeforces 1153D Serval and Rooted Tree (简单树形DP)

    <题目链接> 题目大意: Serval拥有的有根树有n个节点,节点1是根. Serval会将一些数字写入树的所有节点.但是,有一些限制.除叶子之外的每个节点都有一个写入操作的最大值或最小值 ...

  4. Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)

    题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最 ...

  5. Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)

    题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的 ...

  6. cf-Round551-Div2-D. Serval and Rooted Tree(DP)

    题目链接:https://codeforces.com/contest/1153/problem/D 题意:有一棵树,给定结点数n,在每个结点上的操作(max:表示该结点的number为其孩子结点中的 ...

  7. CF 551 D.Serval and Rooted Tree 树形DP

    传送门:http://codeforces.com/contest/1153/problem/D 思路: 这道题想了一天,突发奇想,就是维护每个点两个值,第几大和第几小,就可以有传递性了. #incl ...

  8. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  9. 【Codeforces】Codeforces Round #551 (Div. 2)

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

随机推荐

  1. google 跨域解决办法

    --args --disable-web-security --user-data-dir

  2. Google SRE

    SRE_百度百科 https://baike.baidu.com/item/SRE/1141123 我们离Google SRE还有多远? - 简书https://www.jianshu.com/p/6 ...

  3. ajax属性详解

    https://blog.csdn.net/mooncom/article/details/52402836 资料库: $.ajaxSetup()方法为将来的ajax请求设置默认值. http://w ...

  4. Module build failed: Error: Cannot find module 'babel-runtime/core-js/get-it

    npm i babel-loader@7.1.5 -D

  5. Linux服务器初步配置流程

    一.root登录 首先使用root用户登录远程主机: ssh -p prot root@host 这时命令行会输出类似的信息: The authenticity of host '[23.105.21 ...

  6. P1462 通往奥格瑞玛的道路

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...

  7. 使用 PHP SOAP 来创建一个简单的 Web Service。

    访问: http://www.debug.com/php-soap-demo.php?client=22 结果: apache: <VirtualHost _default_:80> Do ...

  8. 「译」图解 ArrayBuffers 和 SharedArrayBuffers

    作者:Lin Clark 译者:Cody Chan 原帖链接:A cartoon intro to ArrayBuffers and SharedArrayBuffers 这是图解 SharedArr ...

  9. Mac 使用 OpenMP/Clang

    新建 hello.cpp 文件: #include <omp.h> #include <stdio.h> int main() { #pragma omp parallel p ...

  10. Floyd-蒟蒻也能看懂的弗洛伊德算法(当然我是蒟蒻)

    今天来讲点图论的知识,来看看最短路径的一个求法(所有的求法我以后会写,也有可能咕咕咕) 你们都说图看着没意思不好看,那今天就来点情景             暑假,_GC准备去一些城市旅游.有些城市之 ...