程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj3442 Party at Hali-Bula 簡單樹形dp

poj3442 Party at Hali-Bula 簡單樹形dp

編輯:C++入門知識

poj3442 Party at Hali-Bula 簡單樹形dp


http://poj.org/problem?id=3342

Party at Hali-Bula Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5384 Accepted: 1910

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 followingn-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

Tehran 2006
和上一個題基本一樣,多了一個要求。判斷最後的最大值是否唯一。我們用個f數組記錄下,值為一表示唯一 對於dp[i][1]由dp[j][0]轉移來,當f[j][0]有一個為0時那麼f[i][1]就得是0 對於dp[i][0]當dp[j][0]等於dp[j][1]時肯定f[i][0]為0,當dp[j][0]>dp[j][1]就看f[j][0]是否為0,小於就看f[j][1]。 開始的字符串用map處理下簡單方便 代碼:
/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
const int N=205;
mapmp;
vectorg[N];
int dp[N][2];
int f[N][2];  //為1表示以u為根的子樹選和不選u時唯一
void dfs(int u)
{
    for(int i=0;idp[v][0]&&f[v][0]==0) f[u][0]=0;
        else if(dp[v][0]>s1;
        mp[s1]=++tot;
        for(int i=1;i>s1;
            if(mp[s1]==0)
                mp[s1]=++tot;
            cin>>s2;
            if(mp[s2]==0)
                mp[s2]=++tot;
            g[mp[s2]].pb(mp[s1]);
        }
        dfs(1);
        if(dp[1][0]>dp[1][1])
        {
            if(f[1][0]==1) printf("%d Yes\n",dp[1][0]);
            else printf("%d No\n",dp[1][0]);
        }
        else if(dp[1][0]==dp[1][1])
            printf("%d No\n",dp[1][0]);
        else
        {
            if(f[1][1]==1) printf("%d Yes\n",dp[1][1]);
            else printf("%d No\n",dp[1][1]);
        }
    }
    return 0;
}

結尾對捨友早早關燈吐下槽,習慣晚睡加班的我233,雖然習慣不好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved