zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    test
    【转载】ASP.NET MVC 3 —— Model远程验证
    【转载】富有客户端技术之——jQuery EasyUI
    【转载】基于ASP.NET Web Application的插件实现,附DEMO
    【转载】浅谈C#中的延迟加载(1)——善用委托
    【转载】Winform开发框架之权限管理系统
    【转载】基于我的Winform开发框架扩展而成的WCF开发框架
    [转载]10大优秀的移动Web应用程序开发框架推荐
    [转载]C#泛型列表List<T>基本用法总结
    [转载]推荐一个被大家忽视的微软的反跨站脚本库AntiXSS V3.1
  • 原文地址:https://www.cnblogs.com/zzqc/p/8868988.html
Copyright © 2011-2022 走看看