POJ-3659-最小支配集裸题/树形dp

Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7127 Accepted: 2549

Description

Farmer 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 ≤ AN; 1 ≤ BN; AB) 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 B

Output

* Line 1: A single integer indicating the minimum number of towers to install

Sample Input

5
1 3
5 2
4 3
3 5

Sample Output

2

Source

一开始令f[i][0/1]表示属于/不属于支配集的最优解,后来发现这个方程有问题,如果i不属于支配集,他的儿子可能也不属于支配集而是被儿子的儿子所覆盖,这样子就没办法表示了。换句话说状态表示不全。
          就是一道模板最小支配集,用树形dp实现,注意不能转移的状态用inf表示;
 f[i][0]表示i属于支配集且i的子树全被覆盖情况下,最小的支配集。
    f[i][1]表示i不属于支配集,i被至少一个子节点所覆盖,且i的子树全被覆盖情况下,最小的支配集。
    f[i][2]表示i不属于支配集,i不被任何一个子节点覆盖,且i的子树全被覆盖情况下,最小的支配集。
   有转移方程 f[i][0]=1+SUM{MIN{f[j][0],f[j][1],f[j][2]} }
       f[i][1]=f[jx][0]+SUM{min(f[j][0],f[j][1]) | j!=jx}
       f[i][2]=sum{f[j][1] }  /// 注意inf的状态不能被转移,f[i][1],至少有一个i的儿子是由f[j][0]转移而来的才ok。
  
  
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 using namespace std;
 6 #define inf 0x3f3f3f3f
 7 int f[10010][3];
 8 vector<int>g[10010];
 9 void dfs(int u,int fa)
10 {
11     f[u][0]=1;
12     f[u][2]=0;
13     int sum=0,inc=inf;
14     bool flag=0;
15     for(int i=0;i<g[u].size();++i){
16         int v=g[u][i];
17         if(v==fa) continue;
18         dfs(v,u);
19         if(f[v][0]<=f[v][1]){
20             sum+=f[v][0];
21             flag=1;
22         }
23         else{
24             sum+=f[v][1];
25             inc=min(inc,f[v][0]-f[v][1]);
26         }
27         f[u][0]+=min(f[v][0],min(f[v][1],f[v][2]));
28         if(f[v][1]!=inf&&f[u][2]!=inf) f[u][2]+=f[v][1];///inf表示当前节点不会出现此状态
29         else f[u][2]=inf;
30     }
31     if(inc==inf && flag==0) f[u][1]=inf;
32     else{
33         f[u][1]=sum;
34         if(!flag) f[u][1]+=inc;
35     }
36     if(f[u][1]==0) f[u][1]=inf;
37 }
38 int main()
39 {
40     int n,i,j,k,u,v;
41     while(cin>>n){
42         for(i=1;i<n;++i){
43             scanf("%d%d",&u,&v);
44             g[u].push_back(v);
45             g[v].push_back(u);
46         }
47         dfs(1,0);
48         cout<<min(f[1][0],f[1][1])<<endl;
49         for(i=1;i<=n;++i) g[i].clear();
50     }
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/zzqc/p/8868988.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值