【BZOJ】1596: [Usaco2008 Jan]电话网络
【算法】树上贪心
【题解】
因为一个点必须被覆盖,那么它如果没有被子树节点覆盖的话,就覆盖它的父节点。
从叶子开始贪心。
注意,如果它自己已经被选了就不需要选父节点了。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cctype>
using namespace std;
const int maxn=;
struct edge{int v,from;}e[maxn*];
int first[maxn],n,tot,ans,g[maxn]; int read()
{
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
void insert(int u,int v)
{tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
void dfs(int x,int fa){
bool ok=;
for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa){
dfs(e[i].v,x);
if(g[e[i].v])ok=;
}
if(!ok&&!g[x]&&!g[fa])g[fa]=,ans++;
}
int main(){
n=read();
int u,v;
for(int i=;i<n;i++){
u=read();v=read();
insert(u,v);insert(v,u);
}
ans=;
dfs(,);
printf("%d",ans);
return ;
}
【BZOJ】1596: [Usaco2008 Jan]电话网络的更多相关文章
- BZOJ 1596: [Usaco2008 Jan]电话网络
Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无 ...
- bzoj 1596: [Usaco2008 Jan]电话网络【贪心】
dfs,如果一个点的儿子.本身.父亲都没有塔,就在父亲上建一个 原理不明-- #include<iostream> #include<cstdio> using namespa ...
- BZOJ 1596: [Usaco2008 Jan]电话网络 树形DP
挺经典的,细节需要特别注意一下 Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s& ...
- 1596: [Usaco2008 Jan]电话网络
1596: [Usaco2008 Jan]电话网络 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 601 Solved: 265[Submit][S ...
- 【BZOJ】1596: [Usaco2008 Jan]电话网络(树形dp+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1596 一开始交了个貌似正确的dp,wa了. 我只考虑了儿子覆盖的情况,没有考虑父亲QAQ 那么我们要 ...
- BZOJ1596: [Usaco2008 Jan]电话网络
1596: [Usaco2008 Jan]电话网络 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 513 Solved: 232[Submit][S ...
- Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset
1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 891 Solved: 590 ...
- BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛( floyd )
对于第 i 头牛 , 假如排名比它高和低的数位 n - 1 , 那么他的 rank 便可以确定 . floyd -------------------------------------------- ...
- BZOJ 1594 [Usaco2008 Jan]猜数游戏(线段数)
1594: [Usaco2008 Jan]猜数游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 626 Solved: 260[Submit][S ...
随机推荐
- SpringBoot学习:IDEA中快速搭建springboot项目
项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)IDEA中创建maven web项目 创建好项目后设置项目的编译路径: (二)引入 ...
- dom知识总结
一.dom节点的关系及遍历 element.firstChild; 如果节点为已知节点的第一个子节点就可以使用这个方法.此方法可以递归进行使用 element.firstChild.firstChil ...
- 基于阿里云服务器Linux系统部署JavaWeb项目
前段时间刚完成一个JavaWeb项目,想着怎么部署到服务器上,边学边做,花了点时间终于成功部署了,这里总结记录一下过程中所遇到的问题及解决方法.之所以选择阿里云,考虑到它是使用用户最多也是最广泛的云服 ...
- PowerDesigner 使用记录
使用PowerDesigner 16.5 设计数据库表结构的操作过程: 第一步:打开PowerDesigner工具 第二步:创建一个新的数据模型,选择 File -> New Model -&g ...
- CentOs 版本名字说明
What images are in this directory CentOS-6.3-x86_64-netinstall.iso This is the network install and r ...
- coreos install megacli
基于官方的coreos ramdisk安装dell raid管理工具,其版本为debian8 jessie root@c64c7df05677:/# more /etc/apt/sources.lis ...
- [推荐]spring cloud 详解
http://blog.csdn.net/column/details/15197.html
- linux备忘录-账号管理与ACL权限设定
知识 账号管理中的一些文件结构 /etc/passwd 每一行的内容都为下面结构 账号名称:密码:UID:GID:用户信息说明:家目录:shell ---- UID ---- -- 0 -> 代 ...
- STL中list的erase()方法
http://www.cnblogs.com/gshlsh17/ rase()方法是删除iterator指定的节点 但是要注意的是在执行完此函数的时候iterator也被销毁了 这样的话关于it ...
- hw_breakpoint使用方法
hw_breakpoint 使用方法 kprobe在 do_page_fault 函数中不能使用,那么如果真要在这里打点怎么办呢?看看hw_breakpoint是否可用: 事实证明,即便 hw_bre ...