题目链接

BZOJ1596

题解

先抽成有根树

设\(f[i][0|1][0|1]\)表示以\(i\)为根,儿子都覆盖了,父亲是否覆盖,父亲是否建塔的最少建塔数

转移一下即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 10005,maxm = 100005,INF = 10000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int f[maxn][2][2],n,fa[maxn];
int h[maxn],ne,de[maxn];
struct EDGE{int to,nxt;}ed[maxn << 1];
inline void build(int u,int v){
ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
ed[++ne] = (EDGE){u,h[v]}; h[v] = ne;
de[u]++; de[v]++;
}
void dfs(int u){
f[u][1][1] = 1;
f[u][0][0] = 0;
if (u != 1 && de[u] == 1){
f[u][1][0] = INF;
return;
}
else f[u][1][0] = 0;
int flag = false;
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; dfs(to);
f[u][0][0] += f[to][1][0];
if (f[to][1][1] <= f[to][1][0]){
flag = true;
f[u][1][0] += f[to][1][1];
}
else f[u][1][0] += f[to][1][0];
f[u][1][1] += min(f[to][0][0],min(f[to][1][0],f[to][1][1]));
}
if (!flag){
int mn = INF;
Redge(u) if ((to = ed[k].to) != fa[u]){
mn = min(mn,f[to][1][1] - f[to][1][0]);
}
f[u][1][0] += mn;
}
}
int main(){
n = read();
for (int i = 1; i < n; i++)
build(read(),read());
dfs(1);
printf("%d\n",min(f[1][1][1],f[1][1][0]));
return 0;
}

BZOJ1596 [Usaco2008 Jan]电话网络 【树形dp】的更多相关文章

  1. 【bzoj1596】[Usaco2008 Jan]电话网络 树形dp

    题目描述 Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无线电通讯塔,来 ...

  2. BZOJ 1596: [Usaco2008 Jan]电话网络 树形DP

    挺经典的,细节需要特别注意一下 Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s& ...

  3. BZOJ1596: [Usaco2008 Jan]电话网络

    1596: [Usaco2008 Jan]电话网络 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 513  Solved: 232[Submit][S ...

  4. [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)

    传送门 1.树形DP #include <cstdio> #include <cstring> #include <iostream> #define N 1000 ...

  5. 1596: [Usaco2008 Jan]电话网络

    1596: [Usaco2008 Jan]电话网络 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 601  Solved: 265[Submit][S ...

  6. 【bzoj1596】[Usaco2008 Jan]电话网络

    题目描述 Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无线电通讯塔,来 ...

  7. USACO2008 Jan 电话网络

    Time Limit: 10 Sec Memory Limit: 162 MB Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶 ...

  8. 【BZOJ】1596: [Usaco2008 Jan]电话网络(树形dp+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1596 一开始交了个貌似正确的dp,wa了. 我只考虑了儿子覆盖的情况,没有考虑父亲QAQ 那么我们要 ...

  9. BZOJ 1596: [Usaco2008 Jan]电话网络

    Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无 ...

随机推荐

  1. Net Core学习笔记

    Net Core 官网:https://dotnet.github.io/ Net Core Api: https://docs.microsoft.com/en-us/dotnet/api/?vie ...

  2. dva 路由跳转

    1.从props取出并传递history 取 const { history } = this.props 用 <button onClick={ () => history.push(' ...

  3. ThinkDev.Data更新日志

    2013-09-29 10:001.重构Where.And.Or.Having.JoinTable代码,新增条件组合查询QueryGroup2.1.1.2.0 2013-09-04 09:001.修复 ...

  4. C 判断成绩是否及格

    #include <stdio.h> int main(int argc, char **argv) { // 新建两个变量  pass代表及格分数的固定变量 score代表学生成绩的一个 ...

  5. Grid 网格布局

    CSS 网格布局(Grid Layout) 是CSS中最强大的布局系统. 这是一个二维系统,这意味着它可以同时处理列和行,不像 flexbox 那样主要是一维系统. 你可以通过将CSS规则应用于父元素 ...

  6. Java学习笔记-序

    最近开始学习java了,上班看书看得经常瞌睡,有时候想起来觉得挺重要的知识点想记在哪里又害怕忘记了,于是乎突然想到了博客园,所以今天上午就决定记在院子里了,先写了8是因为已经看到第八章了(读的是Jav ...

  7. 收割大厂offer需要具备的条件

    转载出处 本人也一直在关注互联网,觉得还是有些了解.互联网要求是越来越高了,竞争的人太多了,不过你不用担心,个人觉得,你到了中层的水平,拿二线offer应该没问题,人多也有人多的好处,我比别人多努力一 ...

  8. Redhat linux 安装SVN服务器 CollabNetSubversionEdge

    请仔细阅读安装包自带的readme文件! ================================================= 1. 先去官网,找安装包: http://subversi ...

  9. Python面向对象-访问限制

    在Class内部,可以有字段,方法和属性,而外部代码可以通过直接调用实例变量的方法来操作数据, (1)私有普通字段 比如对于下面的Student类,name字段可以在外面通过对象进行直接访问: cla ...

  10. SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)

    Description You are given N circles and expected to calculate the area of the union of the circles ! ...