hdu 2412 基础树形DP 树的最大独立集

Party at Hali-Bula

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2139    Accepted Submission(s): 753


Problem Description
Dear Contestant,

I'm going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers, but imagine how an employee can enjoy a party when he finds his boss among the guests! So, I decide not to invite both an employee and his/her boss. The organizational hierarchy at BCM is such that nobody has more than one boss, and there is one and only one employee with no boss at all (the Big Boss)! Can I ask you to please write a program to determine the maximum number of guests so that no employee is invited when his/her boss is invited too? I've attached the list of employees and the organizational hierarchy of BCM.

Best,
--Brian Bennett

P.S. I would be very grateful if your program can indicate whether the list of people is uniquely determined if I choose to invite the maximum number of guests with that condition.
 

Input
The input consists of multiple test cases. Each test case is started with a line containing an integer n (1 ≤ n ≤ 200), the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n-1 lines contains the name of an employee together with the name of his/her boss. All names are strings of at least one and at most 100 letters and are separated by blanks. The last line of each test case contains a single 0.
 

Output
For each test case, write a single line containing a number indicating the maximum number of guests that can be invited according to the required condition, and a word Yes or No, depending on whether the list of guests is unique in that case.
 

Sample Input
  
  
6 Jason Jack Jason Joe Jack Jill Jason John Jack Jim Jill 2 Ming Cho Ming 0
 

Sample Output
  
  
4 Yes 1 No
 

Source
 

Recommend
lcy


题意:
给定一个树,选择若干点,使得选择的结点中任两点不相邻,求能选结点最大数量。同时判断方案数是否为一。


解法:类似于最小点覆盖,很基础的题。




 当前节点已选,那么其子节点一定不能选, 如果子节点中有达到最优解且方法不止一种的,那么选择当前节点达到最优解也有不止一种方法;
当前节点不选,那么其子节点可以选,也可以不选。对每个子节点的两种选择取其最优解,如果最优解有多解,则不选当前节点也有多解。
当前节点的最优解 是其子节点的最优解的和,所以每个子节点都必须保证最优解,这就是以上 是否有多解 判断方法的依据
dp[i][choose]表示以i为根节点的子树的最优解,并且 是否选择i。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= 200+10   ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123
int n,cnt;
string s1,s2;
int fir[maxn];
int nex[maxn];
bool many[maxn][2];
int dp[maxn][2];
map<string ,int >id;

inline void add_son(int s,int t)
{
       nex[t]=fir[s];
       fir[s]=t;
}


void DP(int x,int choose)
{

    dp[x][choose]=choose;
    many[x][choose]=0;
    for(int y=fir[x];~y;y=nex[y])
    {

        DP(y,1);
        DP(y,0);

        if(!choose)
        {
            dp[x][choose]+=max(dp[y][choose],dp[y][choose^1]);
            many[x][choose]|=  (  dp[y][choose]>dp[y][choose^1]&& many[y][choose]||
                      dp[y][choose]<dp[y][choose^1]  && many [y][choose^1]  ||
                      dp[y][choose]==dp[y][choose^1]              );
        }

        else
        {
            dp[x][choose]+=dp[y][choose^1];
            many[x][choose]|=many[y][choose^1];
        }

    }




}
int main()
{
    int x,y;
    while(~scanf("%d",&n)&&n)
    {
        id.clear();
        cnt=0;
        cin>>s1;
        id[ s1]=++cnt;
        memset(fir,-1,sizeof fir);
        for(int i=1;i<=n-1;i++)
        {
         cin>>s1>>s2;
         int id1,id2;
         if(!id.count(s1))
         {
              id[s1]=++cnt;

         }
         if( !id.count(s2))
         {
             id[s2]=++cnt;
         }
         id1=id[s1];
         id2=id[s2];
         add_son(id2,id1);

        }
        DP(1,1  );
        DP(1,0  );
        printf("%d ",max(dp[1][0],dp[1][1]) );
        puts(  (  dp[1][1]>dp[1][0]&& many[1][1]||
                      dp[1][1]<dp[1][0]  && many [1][0]  ||
                      dp[1][1]==dp[1][0]              )             ?   "No":"Yes");

    }



    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值