洛谷P2996 [USACO10NOV]拜访奶牛Visiting Cows
树形dp
设f[i][j]表示走到第i号节点的最大权值
j为0/1表示这个点选或者不选
如果这个点不选 就从他的子树里的选或者不选选最大
如果这个点选 就加上他子树的不选
f[x][0] += max(f[to][1], f[to][0]);
f[x][1] += f[to][0];
注意第二维要开到2
Code:
//设f[i][j]表示选到第i号节点的最大权值
#include <cstdio>
#include <iostream>
using namespace std;
const int N = ;
int f[N][], n, head[N << ], cnt;
struct node {
int nxt, to;
}e[N];
int read() {
int s = , w = ;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') w = -; ch = getchar();}
while(isdigit(ch)) {s = s * + ch - ''; ch = getchar();}
return s * w;
}
void add(int x, int y) {
e[++cnt].nxt = head[x];
e[cnt].to = y;
head[x] = cnt;
}
void dfs(int x, int fa) {
f[x][] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int v = e[i].to;
if(v != fa) {
dfs(v, x);
f[x][] += max(f[v][], f[v][]);
f[x][] += f[v][];
}
}
}
int main() {
n = read();
for(int i = , x, y; i < n; i++) {
x = read(), y = read();
add(x, y), add(y, x);
}
dfs(, );
cout << max(f[][], f[][]) << endl;
return ;
}
洛谷P2996 [USACO10NOV]拜访奶牛Visiting Cows的更多相关文章
- 洛谷 P2996 [USACO10NOV]拜访奶牛Visiting Cows
P2996 传送门 题意: 给你一棵树,每一条边上最多选一个点,问你选的点数. 我的思想: 一开始我是想用黑白点染色的思想来做,就是每一条边都选择一个点. 可以跑两边一遍在意的时候染成黑,第二遍染成白 ...
- [P2996][USACO10NOV]拜访奶牛Visiting Cows (树形DP)
之前写在洛谷,结果没保存,作废…… 听说考前写题解RP++哦 思路 很容易想到是 树形DP 如果树形DP不知道是什么的话推荐百度一下 我在这里用vector储存边 设状态f[i][0]为i点不访问,f ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解
P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
随机推荐
- Office365激活方法(无需密钥)
@echo off title Activate Office 365 ProPlus for FREE - MSGuides.com&cls&echo =============== ...
- Elasticsearch 7.x从入门到精通
Elasticsearch是一个分布式.可扩展.近实时的搜索与数据分析引擎,它能从项目一开始就赋予你的数据以搜索.分析和探索的能力. 通过本专栏的学习,你可以了解到,Elasticsearch在互联网 ...
- 前端开发CSS3——使用方式和选择器
CSS是Cascading Style Sheets(层叠样式表)的简写,用于修饰文档的语言,可以修饰HTML.XML.SVN.每个语言都有每个语法的规则:CSS声明.CSS声明块.CSS选择器.CS ...
- 简单记录(css换行带点与不带点)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue学习指南:第一篇 - vue的介绍
三大主流框架: 1. Vue.js 是目前最火的一个前端框架,react是最流行的前端框架 (react除了开发网站,还可以开发手机app,Vue语法也是可以用于手机App开发的,需要借助于wexx) ...
- 使用 Docker Alpine 镜像安装 nginx
微镜像Alpine,Alpine Linux 是一款独立的⾮商业性的通⽤ Linux 发行版,Alpine Linux 围绕 musl libc 和 busybox 构建,尽管体积很小,Apline ...
- JDBC学习笔记一
JDBC学习笔记一 JDBC全称 Java Database Connectivity,即数据库连接,它是一种可以执行SQL语句的Java API. ODBC全称 Open Database Conn ...
- 5. [mmc subsystem] mmc core(第五章)——card相关模块(mmc type card)
零.说明(重要,需要先搞清楚概念有助于后面的理解) 1.mmc core card相关模块为对应card实现相应的操作,包括初始化操作.以及对应的总线操作集合.负责和对应card协议层相关的东西. 这 ...
- UVa 202 Repeating Decimals 题解
The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle ...
- E03 【餐厅】Can I book a table for two for this evening,please?
核心句型 Can I book a table for two for this evening,please? 我能预定一张今晚的双人桌吗? What time do you want to hav ...