UVA 1220 / HDOJ 2412 Party at Hali-Bula 树形DP


树的最大独立集+判断唯一性


Party at Hali-Bula
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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(1n200) , 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 orNo, 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

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: More Advanced Topics :: More Advanced DP Techniques ::  DP level 4
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 9. Dynamic Programming ::  Examples
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: More Advanced Topics :: More Advanced Dynamic Programming ::  The More Challenging Ones

Submit Status




/* ***********************************************
Author        :CKboss
Created Time  :2015年02月15日 星期日 19时06分26秒
File Name     :UVA1220.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=300;

int n,cnt;
string name1,name2;
vector<int> vi[maxn];
map<string,int> msi;

bool vised[maxn][2];
struct Node
{
	int val,unique;
}dp[maxn][2];

int getID(string name)
{
	if(msi[name]!=0) return msi[name];
	return msi[name]=cnt++;
}

void init()
{
	msi.clear();
	cnt=1;
	for(int i=0;i<=n+10;i++) vi[i].clear();
	memset(vised,0,sizeof(vised));
}

Node dfs(int now,int kind)
{
	if(vised[now][kind]) return dp[now][kind];
	int sz=vi[now].size();

	/// 选择now时 dp[now][1]
	if(kind==1)
	{
		if(sz==0)
		{
			dp[now][1].val=1;
			dp[now][1].unique=1;
			vised[now][1]=true;
			return dp[now][1];
		}
		bool flag=true;
		int sum=1;
		for(int i=0;i<sz;i++) 
		{
			int v=vi[now][i];
			Node nd = dfs(v,0);
			if(nd.unique==false) flag=false;
			sum+=nd.val;
		}
		dp[now][1].val=sum;
		dp[now][1].unique=flag;
		vised[now][1]=true;
		return dp[now][1];
	}
	/// 不选择now时
	if(kind==0)
	{
		if(sz==0)
		{
			dp[now][0].val=0;
			dp[now][0].unique=1;
			vised[now][0]=true;
			return dp[now][0];
		}

		bool flag=true;
		int sum=0;
		for(int i=0;i<sz;i++)
		{
			int v=vi[now][i];
			Node nd0=dfs(v,0);
			Node nd1=dfs(v,1);
			if(nd0.val==nd1.val) 
			{
				flag=false;
				sum+=nd0.val;
			}
			else if(nd0.val<nd1.val)
			{
				/// nd1
				if(nd1.unique==false) flag=false;
				sum+=nd1.val;
			}
			else if(nd0.val>nd1.val)
			{
				/// nd0
				if(nd0.unique==false) flag=false;
				sum+=nd0.val;
			}
		}
		dp[now][0].val=sum;
		dp[now][0].unique=flag;
		vised[now][0]=true;
		return dp[now][0];
	}
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	while(scanf("%d",&n)!=EOF&&n)
	{
		init();
		cin>>name1;
		getID(name1);
		for(int i=0;i<n-1;i++)
		{
			cin>>name1>>name2;
			int u=getID(name1);
			int v=getID(name2);
			vi[v].push_back(u);
		}
		bool flag=false;
		int ans=0;
		Node nd1=dfs(1,1);
		Node nd0=dfs(1,0);
		if(nd1.val==nd0.val)
		{
			ans=nd1.val;
			flag=false;
		}
		else if(nd1.val<nd0.val)
		{
			ans=nd0.val;
			flag=nd0.unique;
		}
		else if(nd1.val>nd0.val)
		{
			ans=nd1.val;
			flag=nd1.unique;
		}
		printf("%d ",ans);
		if(flag) puts("Yes");
		else puts("No");
	}
    return 0;
}



你可能感兴趣的:(UVA 1220 / HDOJ 2412 Party at Hali-Bula 树形DP)