P2996

传送门

题意:

给你一棵树,每一条边上最多选一个点,问你选的点数.

我的思想:

一开始我是想用黑白点染色的思想来做,就是每一条边都选择一个点.

可以跑两边一遍在意的时候染成黑,第二遍染成白,取一个最大值.

就可以得到\(30\)分的高分.

#include <bits/stdc++.h>
#define N 100010
#define M 1010
#define _ 0 using namespace std;
int n, tot, ans, add_edge, color[N], head[N];
struct node {
int next, to;
}edge[N]; int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
} void add(int from, int to) {
edge[++add_edge].next = head[from];
edge[add_edge].to = to;
head[from] = add_edge;
} void dfs(int x, int fx) {
if (color[fx] == 0) {
color[x] = 1;
tot++;
}
for (int i = head[x]; i; i = edge[i].next) {
int to = edge[i].to;
if (to == fx) continue;
dfs(to, x);
}
} int main() {
n = read();
int point;
for (int i = 1, x, y; i < n; i++) {
x = read(), y = read();
add(x, y), add(y, x);
point = x;
}
dfs(point, 0);
ans = max(ans, tot);
memset(color, 0, sizeof (color));
tot = 0, color[0] = 1;
dfs(point, 0);
cout << max(ans, tot);
}

很明显这样做是错误的.来看这样一组样例.



按照上述方法跑出来就是\(5\),显然答案是\(7\).然后我就是这样被学长\(hack\)了.

然后就问了学长树形\(DP\).

正确思路:

我们设\(dp[i][1/0]\)来表示\(i\)与\(i\)的子树在\(i\),选还是不选,时的最大权值.

然后又因为在\(dp[i][1]\)时他的子节点不能选\(dp[to][1]\).

在\(dp[i][0]\)时都可以选.我们就可以得到这样的转移方程(用\(to\)来表示\(i\)的子节点):

\[dp[x][0] += max(dp[to][1], dp[to][0]);
\]

\[dp[x][1] += dp[to][0];
\]

然后就做完了.

code :

#include <bits/stdc++.h>
#define N 100010
#define M 50010
#define _ 0 using namespace std;
int n, add_edge, head[N];
int dp[M][2];
struct node {
int next, to;
}edge[N]; int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
} void add(int from, int to) {
edge[++add_edge].next = head[from];
edge[add_edge].to = to;
head[from] = add_edge;
} void dfs(int x, int fx) {
dp[x][1] = 1;
for (int i = head[x]; i; i = edge[i].next) {
int to = edge[i].to;
if (to == fx) continue;
dfs(to, x);
dp[x][0] += max(dp[to][1], dp[to][0]);
dp[x][1] += dp[to][0];
}
} int main() {
n = read();
for (int i = 1, x, y; i < n; i++) {
x = read(), y = read();
add(x, y), add(y, x);
}
dfs(1, 0);
cout << max(dp[1][0], dp[1][1]);
}

洛谷 P2996 [USACO10NOV]拜访奶牛Visiting Cows的更多相关文章

  1. 洛谷P2996 [USACO10NOV]拜访奶牛Visiting Cows

    题目 树形dp 设f[i][j]表示走到第i号节点的最大权值 j为0/1表示这个点选或者不选 如果这个点不选 就从他的子树里的选或者不选选最大 如果这个点选 就加上他子树的不选 f[x][0] += ...

  2. [P2996][USACO10NOV]拜访奶牛Visiting Cows (树形DP)

    之前写在洛谷,结果没保存,作废…… 听说考前写题解RP++哦 思路 很容易想到是 树形DP 如果树形DP不知道是什么的话推荐百度一下 我在这里用vector储存边 设状态f[i][0]为i点不访问,f ...

  3. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...

  4. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  5. 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解

    P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...

  6. 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  7. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  8. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...

  9. 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...

随机推荐

  1. 【学习笔记】字符串—马拉车(Manacher)

    [学习笔记]字符串-马拉车(Manacher) 一:[前言] 马拉车用于求解连续回文子串问题,效率极高. 其核心思想与 \(kmp\) 类似:继承. --引自 \(yyx\) 学姐 二:[算法原理] ...

  2. WPF WebBrowser抑制Suppress 弹出 脚本错误 对话框 但是样式改变 需要继续改善

    1.添加引用 using System.Reflection;using System.Windows.Controls; 2.静态类扩展方法(this) public static class We ...

  3. Linq与委托

    using System; using System.Linq; using System.Reflection; using Stuglxt_Models; namespace ConsoleApp ...

  4. ionic 股票列表 网络读取数据,实现下拉刷新,上拉加载

    html: <ion-header> <ion-toolbar> <ion-title> 股票 </ion-title> </ion-toolba ...

  5. Common Lisp中的读取宏 ' #' `( , ,@) #( ) #na( ) #<OBJECT> :Keyword

    当你把  xx 当做符号使用时   'xx  ,  这个符号是没有任何函数/变量语义的, 仅仅是一个 符号而已(就像一个string一样) 但你可以对这个string有其他的用法,比如使用它所bind ...

  6. 用chrome的snippets片段功能创建页面js外挂程序,从控制台创建js小脚本

    用chrome的snippets片段功能创建页面js外挂程序,从控制台创建js小脚本 Chrome的snippets是小脚本,还可以创作并在Chrome DevTools的来源面板中执行.可以访问和从 ...

  7. 判读是不是对象字面量(纯对象)。对象字面量创建方式有{}、new Object()创建

    //判读是否是自身属性 function isHasPro(obj,pro){ return obj.hasOwnProperty(pro) ? true : false; } //判读是不是对象字面 ...

  8. disable_function绕过--利用LD_PRELOAD

    0x00 前言 有时候直接执行命令的函数被ban了,那么有几种思路可以bypass 1.使用php本身自带的能够调用外部程序的函数 2.使用第三方插件入(如imagick) 但是这两种无非就是利用ph ...

  9. 2019CISCN web题赛-JustSoSo;love_math(复现)

    0x00前言 这几天从网上找个CMS源码开始练习审计,盯着众多的代码debug调呀调头晕脑胀的,还不错找到个文件读取和一个ssrf... 上月底结束的CISCN线上赛,web四道,仔细研究的2道,做出 ...

  10. 【转载】Gradle学习 第三章:教程

    转载地址:http://ask.android-studio.org/?/article/15 3.1. Getting Started 入门The following tutorials intro ...