Problem

给你一棵树,可以在每个点上选择造塔或不造,每座塔可以覆盖这个节点和相邻节点,问覆盖整棵树的最小塔数。

Solution

看到这道题的第一眼,我就觉得是一题贪心题,但看见出题的时候分类在树形DP,于是就没仔细想贪心。

树形DP:f[u][0]表示u被其儿子覆盖,f[u][1]表示u上有塔,f[u][2]表示u被其父亲覆盖,转移显然

贪心:我们dfs到叶子节点时,尽量贪心覆盖它的父亲

Notice

树形DP的状态确实很难想到

Code

树形DP

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; ++i)
#define per(i, a, b) for (reg i = a; i >= b; --i)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 10000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct node
{
int vet, next;
}edge[2 * N + 5];
int ans = 0, flag[N + 5], num = 0, head[N + 5];
inline void add(const int &u, const int &v)
{
edge[++num].vet = v, edge[num].next = head[u], head[u] = num;
edge[++num].vet = u, edge[num].next = head[v], head[v] = num;
}
inline void dfs(reg u, reg fa)
{
reg tt = 0;
travel(i, u)
{
int v = edge[i].vet;
if (v == fa) continue;
dfs(v, u);
if (flag[v]) tt = 1;
}
if (!tt && !flag[u] && !flag[fa])
{
ans++;
flag[fa] = 1;
}
}
int sqz()
{
reg n = read();
rep(i, 1, n - 1) add(read(), read());
dfs(1, 0);
write(ans); puts("");
return 0;
}

贪心

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e8, N = 10000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int f[N + 5][3], head[N + 5];
int num = 0;
struct node
{
int vet, next;
}edge[2 * N + 5];
void add(int u, int v)
{
edge[++num].vet = v;
edge[num].next = head[u];
head[u] = num;
edge[++num].vet = u;
edge[num].next = head[v];
head[v] = num;
}
void dp(int u, int fa)
{
int sum = 0;
f[u][1] = 1, f[u][0] = INF;
travel(i, u)
{
int v = edge[i].vet;
if (v == fa) continue;
dp(v, u);
f[u][0] = min(f[u][0], f[v][1] - min(f[v][1], f[v][0]));
f[u][1] += min(f[v][0], min(f[v][1], f[v][2]));
f[u][2] += f[v][0];
sum += min(f[v][0], f[v][1]);
}
f[u][0] += sum;
}
int sqz()
{
int n = read();
rep(i, 1, n - 1) add(read(), read());
dp(1, 0);
printf("%d\n", min(f[1][0], f[1][1]));
return 0;
}

[BZOJ1596]电话网络的更多相关文章

  1. DP——由蒟蒻到神犇的进阶之路

    开始更新咯 DP专题[题目来源BZOJ] 一.树形DP 1.bzoj2286消耗战 题解:因为是树形结构,一个点与根节点不联通,删一条边即可, 于是我们就可以简化这棵树,把有用的信息建立一颗虚树,然后 ...

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

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

  3. BZOJ1596 [Usaco2008 Jan]电话网络 【树形dp】

    题目链接 BZOJ1596 题解 先抽成有根树 设\(f[i][0|1][0|1]\)表示以\(i\)为根,儿子都覆盖了,父亲是否覆盖,父亲是否建塔的最少建塔数 转移一下即可 #include< ...

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

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

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

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

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

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

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

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

  8. 3336 /P1948电话网络(二分答案)

    3336 电话网络  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold       题目描述 Description 由于地震使得连接汶川县城电话线全部损坏,假如你是 ...

  9. USACO2008 Jan 电话网络

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

随机推荐

  1. Apache Maven 3.6.1配置安装

    Apache Maven 3.6.1配置安装 一.下载 maven下载地址:http://maven.apache.org/download.cgi 二.安装 1,解压即可用 2,环境变量配置 MAV ...

  2. 腾讯这套SpringMvc面试题你了解多少?(面试必备)

    1.什么是 SpringMvc? 答:SpringMvc 是 spring 的一个模块,基于 MVC 的一个框架,无需中间整合层来整 2.Spring MVC 的优点: 答: 1)它是基于组件技术的. ...

  3. 输入时间参数获取rds备份集信息

    1.脚本 [root@localhost tmp]# more geturl_test.py #!/usr/bin/env python #coding=utf- import os, json, u ...

  4. Labels & Codes

     Labels & Codes List of Codes Adjectives Nouns Verbs Other labels Adjectives adjective A word th ...

  5. Android系统分析之运营商显示流程分析之运营商信息的读取流程二

    运营商显示流程分析之运营商信息的读取流程 一. SIM卡运营商信息的读取 从前面的 运营商信息的获取和赋值 可以知道SIM卡运营商的赋值最终是在 SIMRecords 中完成的, 而SIM卡信息的相关 ...

  6. 【用户权限】MongoDB用户权限

    一.数据库用户角色: read:允许用户读取指定数据库readWrite:允许用户读写指定数据库 二.数据库管理角色:dbAdmin.dbOwner.userAdmin: dbAdmin:允许用户在指 ...

  7. 6、Docker存储卷

    Why Data Volumes?  来自马哥教育 Data volumes Volume types  绑定挂载卷:在宿主机和容器上各指明绑定路径才能进行绑定. docker管理卷:在容器内指定挂载 ...

  8. 应对 Visual Stdio 编译时出现错误:常量中有换行符

    笔者最近用 Visual Stdio 时,发现一个问题,在某一次写完语言进行编绎运行时,出现了以下错误: C2001错误:变量中有换行符 C2413错误:语法错误 缺少")"(在& ...

  9. (js) 字符串和数组的常用方法

    JS中字符串和数组的常用方法 JS中字符串和数组的常用方法 js中字符串常用方法 查找字符串 根据索引值查找字符串的值 根据字符值查找索引值 截取字符串的方法 字符串替换 字符串的遍历查找 字符串转化 ...

  10. [MySQL] timestamp和datetime的区别

    建表语句如下: create table strong_passwd_whitelist( id int unsigned not null auto_increment, email_id int ...