poj 3342 Party at Hali-Bula 树状dp

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

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

BB将要邀请BCM的员工参加一个party来庆祝他的退休。还是每个人不能同其直接boss一起参加。要求使参加人数最多,并要求判断方案是否唯一。

人数最多比较简单。

  //ti 为 t 的子节点。0表示该节点不参加,1表示参加

  dp[t][0] = sum(max(dp[ti][0], dp[ti][1]));

  dp[t][1] = sum(dp[ti][0]) + 1;

方案是否唯一就有点麻烦了。

不过,我们可以知道,方案不唯一的必定是在某个节点上存在dp[t][0] == dp[t][1] 的情况。

所以我们用ok[t][0] 表示不取t节点的情况下是否冲突/* ok[t][1] 表示取t节点的情况下是否冲突 */ 。“0” 表示没有冲突。

那么如果节点不取的话,冲突的情况是存在dp[ti][0] == dp[ti][1] 或者其选择的子节点(选或者不选)已经冲突了。

如果取该节点的话,冲突情况是存在一个子节点不选的情况下冲突。

  ok[t][0] := if(dp[ti][0] == dp[ti][1]) || ok[ti][p] == 1;//p为该子节点选或者不选

  ok[t][1] := if(ok[ti][0] == 1);

最后如果有dp[root][0] == dp[root][1] 或者选择0/1的情况下ok[root][0/1] == 1 时输出 “No” ,其他“Yes”。

#include <map>

#include <vector>

#include <cstdio>

#include <string>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;



int n;

std::map<string, int> m;

std::vector<int > v[202];

int dp[202][2];

int ok[202][2]; // "0" means is unique



void Tdp(int t) {

    int flag = 0;

    if(v[t].size() == 0) {

        dp[t][0] = 0;

        dp[t][1] = 1;

        ok[t][0] = ok[t][1] = 0;

        return ;

    }

    int sum0 = 0, sum1 = 0;

    for (int i=0; i<v[t].size(); i++) {

        Tdp(v[t][i]); 

        sum0 += max(dp[v[t][i]][0], dp[v[t][i]][1]);

        sum1 += dp[v[t][i]][0];

        if (ok[v[t][i]][0] == 1) ok[t][1] = 1;

        if (dp[v[t][i]][0] < dp[v[t][i]][1] && ok[v[t][i]][1] == 1) flag = 1; 

        else if (dp[v[t][i]][0] == dp[v[t][i]][1] ) flag = 1;

        else if (dp[v[t][i]][0] > dp[v[t][i]][1] && ok[v[t][i]][0] == 1) flag = 1;

    }

    dp[t][0] = sum0;

    dp[t][1] = sum1 + 1;

    ok[t][0] = flag;

}



void init() {

    m.clear();

    for (int i=0; i<202; i++) {

        v[i].clear();

    }

    memset(dp,0,sizeof(dp));

    memset(ok,0,sizeof(ok));

}

int main () {

    string root;

    while (cin >> n ) {

        if (n == 0) break;

        init();

        cin >> root;

        m[root] = 1;

        string a, b;

        int k = 2;

        for (int i=1; i<n; i++) {

            cin >> a >> b;

            if (m[a] == 0) m[a] = k++;

            if (m[b] == 0) m[b] = k++;

            v[m[b]].push_back(m[a]);

        }

        Tdp(m[root]);

        cout << max(dp[1][0], dp[1][1]) << " ";

        if (dp[1][0] > dp[1][1] && ok[1][0] == 1) cout << "No" <<endl;

        else if (dp[1][1] > dp[1][0] && ok[1][1] == 1) cout << "No" <<endl;

        else if (dp[1][0] == dp[1][1]) cout << "No" <<endl;

        else cout << "Yes" <<endl;

    }

    

    return 0;

}

 

 

 

 

 

你可能感兴趣的:(part)