poj3342-Party at Hail-Bula 树形dp/树的最大独立集

来源:互联网 发布:淘宝客自动采集软件 编辑:程序博客网 时间:2024/04/28 18:47

Party at Hali-Bula
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5683 Accepted: 2030

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

6JasonJack JasonJoe JackJill JasonJohn JackJim Jill2MingCho Ming0

Sample Output

4 Yes1 No

Source

Tehran 2006


紫书例题。

用f数组表示唯一性的dp值得学习。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<map>using namespace std;const int maxn=1e3+7;#define ll long longint n;int fa[maxn];vector<int> so[maxn];int d[maxn][2];int f[maxn][2];int rt;int num;string s, t;map<string, int> node;void init(){    node.clear();    rt=0;    num=0;    for(int i=0;i<maxn;i++) so[i].clear();    memset(d, -1, sizeof(d));    memset(f, -1, sizeof(f));    memset(fa, 0, sizeof(fa));}int ID(string & s){    map<string, int>::iterator it=node.find(s);    if(it != node.end()) return it->second;    node.insert(make_pair(s, ++num));    return num;}void dfs(int  cur){    d[cur][0]=0;    d[cur][1]=1;    f[cur][0]=f[cur][1]=1;    if(so[cur].size() == 0){        d[cur][0]=0;        d[cur][1]=1;        f[cur][0]=f[cur][1]=1;        return ;    }    for(vector<int>::iterator it=so[cur].begin();it != so[cur].end();it++){        if(d[*it][0] == -1) dfs(*it);        if(d[*it][1] == -1) dfs(*it);        d[cur][0] += max(d[*it][0], d[*it][1]);        if(d[*it][0] == d[*it][1]) f[cur][0]=0;        else if(d[*it][0]>d[*it][1]){            if(f[*it][0] == 0) f[cur][0]=0;        }        else {            if(f[*it][1] == 0) f[cur][0]=0;        }        if(d[*it][0] == -1) dfs(*it);        d[cur][1] += d[*it][0];        if(f[*it][0] == 0) {            f[cur][1]=0;        }    }    //if(f[cur][0] == -1) f[cur][0]=1;    //if(f[cur][1] == -1) f[cur][1]=1;}void solve(){    dfs(rt);    printf("%d ", max(d[rt][0], d[rt][1]));    if(d[rt][0]>d[rt][1]){        if(f[rt][0] == 1) printf("Yes\n");        else printf("No\n");    }    else if(d[rt][0]<d[rt][1]){        if(f[rt][1] == 1) printf("Yes\n");        else printf("No\n");    }    else printf("No\n");}int main(){    while(~scanf("%d", &n)&&n){        init();        cin>>s;        rt=ID(s);        for(int i=0;i<n-1;i++){            cin>>s>>t;            int a=ID(s);            int b=ID(t);            fa[a]=b;            so[b].push_back(a);        }        solve();    }}


0 0
原创粉丝点击