POJ 3659 Cell Phone Network 最小支配集
来源:互联网 发布:淘宝打假新规则2016 编辑:程序博客网 时间:2024/04/28 19:04
Cell Phone NetworkTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6442 Accepted: 2323DescriptionFarmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.Input* Line 1: A single integer: N* Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and BOutput* Line 1: A single integer indicating the minimum number of towers to installSample Input51 35 24 33 5Sample Output2SourceUSACO 2008 January Gold
2种解法:
①贪心:
如果u没被覆盖,显然 将u的父亲设置成支配集中的点 是最优选择
从叶子节点开始向上进行贪心选择 dfs一遍记录到达的点的顺序 倒过来选择即可
②树形DP:
考虑3个状态:
d[u][0]=u点为支配集中的点 ,以u为根的子树已经被覆盖时,u为根的子树中有几个支配点
d[u][1]=u不为支配集中的点,u为的根的子树中,至少有一个u的孩子是支配点,覆盖了u 此时u为根的子树有几个支配点
d[u][2]=u不为支配集中的点,u为根的子树中,没有一个u的孩子是支配点,u未被覆盖,此时u为根的子树有几个支配点
显然d[u][0]=∑min(d[v][0],d[v][1],d[v][1]) //(v为u的孩子)
d[u][2]:u没被覆盖 不能有d[v][0]状态的孩子,d[v][2]状态的孩子v无法被覆盖,也不能选,所以: d[u][2]=∑d[v][1] //v为u的孩子
d[u][1]就比较麻烦了 ,显然 d[u][1]状态下 不能有d[v][2]状态的孩子,因为孩子无法被覆盖
当u没有孩子,那u肯定不能被支配 所以d[u][1]=INF
当u有孩子
d[u][1]=∑min(d[v][1],d[v][0])+incinc=min(d[v][0]-d[v][1])//选择了一个支配点做儿子
综上:
d[u][0]=∑min(d[v][0],d[v][1],d[v][1])d[u][2]=∑d[v][1]d[u][1]=∑min(d[v][1],d[v][0])+incinc=min(d[v][0]-d[v][1])
最后 最小支配集=min(d[root][0],d[root][1])
//贪心:#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>inline void read(int&x){ x=0; char t; while(isdigit(t=getchar())) x=x*10+t-'0';}const int N=10000+5;const int M=2*N;struct Edge{ int to,next;}edge[M];int head[N];inline void addEdge(int k,int u,int v){ edge[k]={v,head[u]}; head[u]=k;}int newPos[N];//dfs遍历顺序int father[N];//父亲节点int now=0;void dfs(int u){ newPos[now++]=u; for(int i=head[u];i!=-1;i=edge[i].next){ if(edge[i].to!=father[u]){ father[edge[i].to]=u; dfs(edge[i].to); } }}bool covered[N];//标记点是否被支配集覆盖bool isAns[N];//标记点是否属于最小支配集int greedy(int n){ int ans=0; for(int i=n-1;i>=0;--i){ int pos=newPos[i]; if(false==covered[pos]){//如果当前点没被覆盖 if(false==isAns[father[pos]]){//如果父亲不是支配集的点 设成支配集的点 isAns[father[pos]]=true; ++ans; } covered[pos]=true; covered[father[pos]]=true; covered[father[father[pos]]]=true; } } return ans;}int main(){ //freopen("/home/lu/文档/r.txt","r",stdin); //freopen("/home/lu/文档/w.txt","w",stdout); int n,u,v; scanf("%d",&n); fill(head,head+n+1,-1); for(int i=0;i<n-1;++i){ scanf("%d%d",&u,&v); addEdge(2*i,u,v); addEdge(2*i+1,v,u); } father[1]=1; dfs(1); printf("%d\n",greedy(n)); return 0;}
//树形DP#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>inline void read(int&x){ x=0; char t; while(isdigit(t=getchar())) x=x*10+t-'0';}const int N=10000+5;const int M=2*N;struct Edge{ int to,next;}edge[M];int head[N];inline void addEdge(int k,int u,int v){ edge[k]={v,head[u]}; head[u]=k;}int d[N][3];void DP(int u,int father){ d[u][0]=1; d[u][1]=INF; d[u][2]=0; bool flag=false; int sum=0; for(int i=head[u];i!=-1;i=edge[i].next){ int to=edge[i].to; if(to==father) continue; DP(to,u); d[u][0]+=min(d[to][0],min(d[to][1],d[to][2])); d[u][2]+=d[to][1]; d[u][2]=min(d[u][2],INF); if(d[to][0]<=d[to][1]){ flag=true; sum+=d[to][0]; } else sum+=d[to][1]; } if(flag==false){ for(int i=head[u];i!=-1;i=edge[i].next){ int to=edge[i].to; if(to!=father) d[u][1]=min(d[u][1],sum-d[to][1]+d[to][0]); } } else d[u][1]=sum;}int main(){ //freopen("/home/lu/文档/r.txt","r",stdin); //freopen("/home/lu/文档/w.txt","w",stdout); int n,u,v; scanf("%d",&n); fill(head,head+n+1,-1); for(int i=0;i<n-1;++i){ scanf("%d%d",&u,&v); addEdge(2*i,u,v); addEdge(2*i+1,v,u); } DP(1,1); printf("%d\n",min(d[1][1],d[1][0])); return 0;}
0 0
- POJ 3659 Cell Phone Network【最小支配集 dp && 贪心】
- POJ 3659 Cell Phone Network【最小支配集、树型dp】
- poj 3659 Cell Phone Network (树最小支配集)
- POJ 3659 Cell Phone Network 最小支配集
- poj 3659 Cell Phone Network 树型DP也是树的最小支配集
- poj 3659 Cell Phone Network(树dp+树的最小支配集)
- POJ 3659 Cell Phone Network 最小支配集模板题(树形dp)
- poj 3659 Cell Phone Network(树的最小支配集树形DP)
- poj 3659 Cell Phone Network 树的最小点支配集
- Poj 3659 Cell Phone Network(树的最小支配集)
- POJ3659 Cell Phone Network【最小支配集】【贪心】
- poj3659 Cell Phone Network(最小支配集-树形dp)
- 【POJ】3659 Cell Phone Network 树上的极小支配集——树型DP
- 【POJ3659】Cell Phone Network【树上最小点支配】【TreeDP】
- POJ 3659 Cell Phone Network
- POJ-3659-Cell Phone Network
- POJ 3659 Cell Phone Network
- POJ 3659 Cell Phone Network
- js运动-链式运动、摩擦运动、缓冲运动、运动框架加入缓冲
- .NET MVC5自带模板方法功能解析(3)--ASP.NET Identity
- {算法}Young司机带你轻松KMP
- C. Sanatorium
- 编程规范6
- POJ 3659 Cell Phone Network 最小支配集
- RGB、YUV和YCbCr
- 数据结构之栈go言语实现
- [JZOJ4872]太阳神
- mysql
- 《计算机网络》知识总结-8.TCP中什么是滑动窗口技术?为什么要这个?
- TextView如何限制输入字数
- linux网络编程之UDP
- 用c#在excel中插入图片和设置表格宽度