1. 程式人生 > >POJ - 3342 Party at Hali-Bula (樹形DP+STL)

POJ - 3342 Party at Hali-Bula (樹形DP+STL)

POJ - 3342 Party at Hali-Bula (樹形DP+STL)

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

  • 題目大意:
    某人要舉辦一個聚會,然後要邀請公司的人,要避免員工和他的直系上司同時出現,問最多能邀請多少個人。
    抽象一下模型:有一顆樹,相連線的點不能同時取,問,最多能取多少個點,還要判斷一下取的點的方式是否唯一。
    解釋一下輸入輸出:先輸入n (頂點的個數)然後輸入boss 的名字(根節點) 然後下面n- 1 行每一行輸入兩個名字 a ,b b是a 的直系上司。輸出最多能邀請的人的個數。如果選點唯一,輸出YES 否則 NO。
  • 解題思路:
    我們考慮一個結點,如果這個點不取的話,那麼他的子結點就可以取,或者不取兩種選擇;如果這個點取,那麼他的子節點就一定不能取。
    我們用動態規劃的思想來考慮這個問題,建立一個dp[][]二維陣列,dp[i][0]表示,不選i結點時所能取到的最大個數(最優解) dp[i][1]就是選 i結點時的最優解,所以我們可以得出狀態轉移方程了:
//t是x的子節點
dp[x][0]+=max(dp[t][0],dp[t][1]);//x不選的時候 子節點可以選也可以不選;
		dp[x][1]+=dp[t][0];//x選的時候 子節點一定不能選

-具體實現細節:
1.我們用鄰接表來存圖,因為我們要存的是一棵樹,樹的邊數是n-1 是個稀疏圖,用鄰接表可以大大降低時間複雜度。
2.我們用個map 來將人名和標號聯絡起來。
3.用個set 來放人名,輸入的時候每次遇到一個名字就判斷set裡有沒有,沒有就放進去,然後mp[str]=cnt++;
(2,3配合,來將人名標號化為了方便下面的操作。)
4.如何判斷是否唯一呢? 我們想一下哈,如果dp[i][0]>dp[i][1], 那麼這個時候該結點不取, 那他的子節點就有兩種選擇,選或者不選,如果選,不選的值一樣,那選不選就都可以了 就有兩種選擇了,然後我們遍歷每一個點,找dp[i][0]>dp[i][1]的點,找他的子節點是否存在 dp[j][0]==dp[j][1]的情況,存在就標記個flag 然後跳出

for(int i=1;i<=n;i++)
        {
        	if(dp[i][0]>=dp[i][1])
        	{
        		for(int j=head[i];j!=-1;j=side[j].next)
        		{
        			int t=side[j].to;
        			if(dp[t][0]==dp[t][1])
        			{
        				flag=0;
        				break;
					}
				}
			}
			if(flag==0)
				break;
		}

這個題主要用了樹形DP的思想,然後細節方面處理要用到STL。

  • AC程式碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <cmath>
using namespace std;
const int maxn=210;
struct node{
    int to;
    int next;
}side[maxn*2];
int head[maxn];
int dp[maxn][2];
int cnt=0;
int n;
set<string> s;
map<string,int> mp;
void init()
{
    memset(head,-1,sizeof(head));
    s.clear();
    cnt=0;
}
void add(int x,int y)
{
    side[cnt].to=y;
    side[cnt].next=head[x];
    head[x]=cnt++;
}
void dfs(int x)
{
    if(head[x]==-1)
    {
        dp[x][1]=1;
        dp[x][0]=0;
        return;
    }
    dp[x][0]=0;
    dp[x][1]=1;
    for(int i=head[x];i!=-1;i=side[i].next)
    {
        int t=side[i].to;
        dfs(t);///讓子節點都安排正確
        dp[x][0]+=max(dp[t][0],dp[t][1]);//x不選的時候 子節點可以選也可以不選;
        dp[x][1]+=dp[t][0];//x選的時候 子節點一定不能選
    }
}
int main()
{
    while(cin>>n)
    {
        if(n==0)
            break;
        init();
        int ct=1;
        string boss;
        cin>>boss;
        s.insert(boss);
        mp[boss]=ct++;
        for(int i=0;i<n-1;i++)
        {
            string a,b;
            cin>>a>>b;
            if(s.count(a)==0)
            {
                mp[a]=ct++;
                //cout<<a<<" :"<<mp[a]<<endl;
                s.insert(a);
            }
            if(s.count(b)==0)
            {
                mp[b]=ct++;
              //  cout<<b<<" :"<<mp[b]<<endl;
                s.insert(b);
            }
            
            add(mp[b],mp[a]);
        }
        dfs(1);
        cout<<max(dp[1][0],dp[1][1])<<" ";
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(dp[i][0]>=dp[i][1])
            {
                for(int j=head[i];j!=-1;j=side[j].next)
                {
                    int t=side[j].to;
                    if(dp[t][0]==dp[t][1])
                    {
                        flag=0;
                        break;
                    }
                }
            }
            if(flag==0)
                break;
        }
       if(dp[1][0]==dp[1][1])
            flag=0;
        if(flag==1)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}