挺简单的一个dp

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 10100
using namespace std;
const int INF = 0x3f3f3f3f;
vector<int>G[maxn];
void insert(int be, int en) {
G[be].push_back(en);
}
int n, m;
int h[maxn];
int b[maxn];
int vis[maxn]; int dfs(int x, int fa) {
h[x] = 1;
b[x] = 1;
if (vis[x] == 0) {
b[x] = INF;
}
if (vis[x] == 1) {
h[x] = INF;
}
for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i];
if (p == fa) continue;
dfs(p, x);
h[x] += min(b[p], h[p] - 1);
b[x] += min(h[p], b[p] - 1);
}
return 0;
} int main() {
memset(vis, -1, sizeof(vis));
scanf("%d %d", &n, &m); int op;
for (int i = 1; i <= m; i++) {
scanf("%d", &op);
vis[i] = op;
}
int be, en;
int root = n;
for (int i = 1; i < n; i++) {
scanf("%d %d", &be, &en);
insert(be, en);
insert(en, be);
}
dfs(n, -1);
printf("%d\n", min(h[n], b[n]));
return 0;
}

  

叶子的颜色---经典树上dp的更多相关文章

  1. codevs 1421 秋静叶&秋穣子(树上DP+博弈)

    1421 秋静叶&秋穣子   题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一 起,自然会想到烤红薯 ...

  2. bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...

  3. 【题解】彩色树 51nod 1868 虚树 树上dp

    Prelude 题目在这里:ο(=•ω<=)ρ⌒☆ Solution 蒟蒻__stdcall的第一道虚树题qaq. 首先很容易发现,这个排列是假的. 我们只需要求出每对点之间的颜色数量,然后求个 ...

  4. BZOJ 4027: [HEOI2015]兔子与樱花 树上dp

    4027: [HEOI2015]兔子与樱花 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...

  5. BZOJ_1304_[CQOI2009]叶子的染色_树形DP

    BZOJ_1304_[CQOI2009]叶子的染色_树形DP Description 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白 ...

  6. 洛谷【P2458】[SDOI2006]保安站岗 题解 树上DP

    题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...

  7. 洛谷P4426 毒瘤 [HNOI/AHOI2018] 虚树+树上dp

    正解:虚树+树上dp 解题报告: 传送门! 首先解释一下题意趴,,,语文70pts选手已经开始看不懂题辣QAQ 大概就是个给一个图,求独立集方案,且保证图是联通的,边的数量最多只比点多10 首先思考如 ...

  8. CodeForces 690C2 Brain Network (medium)(树上DP)

    题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...

  9. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

随机推荐

  1. Java练习 SDUT-3422_小金问呀问不会问题

    小金问呀问不会问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 众所周知,C语言的学习是我们程序设计基础的重点和主要 ...

  2. 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的差别和联系

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/weasleyqi/article/details/33396809 1.首先先了解下NSNumber ...

  3. @loj - 2196@「SDOI2014」Lis

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定序列 A,序列中的每一项 Ai 有删除代价 Bi 和附加属性 ...

  4. List of the best open source software applications

    List of the best open source software applications by Ryan • Oct 25th, 2008 • Category: Featured Art ...

  5. pytorch nn.Embedding

    pytorch nn.Embeddingclass torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_no ...

  6. nginx简单使用(windows)

    本篇文章对术语不作讲解 下载nginx 首先,进入nginx官网http://nginx.org/en/download.html. 找到Stable version,此处的版本是稳定版本: 下载完成 ...

  7. 学习微信小程序

    1.从小程序指南文档开始看起:小程序指南 2.开发者工具下载:小程序开发工具

  8. bnu 52037 Escape from Ayutthaya

    Escape from Ayutthaya Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on CodeFo ...

  9. PHP利用纯真IP数据库在本地实现IP地址信息查询

    https://blog.csdn.net/myweishanli/article/details/45098693 准备工作: 建议本地IP地址数据库,请到http://www.cz88.net/这 ...

  10. 不需内测账号,带你体验微信小程序完整开发过程

    不需内测账号,带你体验微信小程序完整开发过程 2016年09月24日 - 作者: SwiftCafe 微信小程序还没正式发布就已经迅速成为大家讨论的焦点,那么大家可能觉得只有收到内测邀请才能体验小程序 ...