Party at Hali-Bula (最大独立集)

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

题意:求一个有根树的最大独立集,并判断最大独立集是否唯一。

题解:树状dp,dp[i][1] 表示以i为根且取第i个点的最大独立集,dp[i][0] 表示以i为根且不取第i个点的最大独立集。判断是否唯一,当某个节点u,不取时,如果存在dp[v][1] == dp[v][0]时,最大独立集不唯一,或dp[1][0] == dp[1][1]时,最大独立集不唯一。

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e4 + 7;
const int maxn =  2e5+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

vector<int>ve[300];
int n, cnt;
int dp[300][2]; //dp[i][1] 表示以i为根且取第i个点的最大独立集
                //dp[i][0] 表示以i为根且不取第i个点的最大独立集

int dfs(int u, int fa){
    ULL sz = ve[u].size();
    for(int i=0; i<sz; i++) {
        int v = ve[u][i];
        if(v != fa) dfs(v, u);
        dp[u][0] += max(dp[v][1], dp[v][0]);
        dp[u][1] += dp[v][0];
    }
    return max(dp[u][0], dp[u][1]);
}

bool check(){  //判断最大独立集是否唯一
    for(int i=1; i<cnt; i++) {
        if(dp[i][0] > dp[i][1]) {
            ULL sz = ve[i].size();
            for(int j=0; j<sz; j++) {
                int v = ve[i][j];
                if(dp[v][0] == dp[v][1]) return false;
            }
        }
    }
    return true;
}

void init(){
    for(int i=0; i<=n; i++){
        dp[i][1] = 1; dp[i][0] = 0;
        ve[i].clear();
    }
}

int main(){
    while(~scanf("%d", &n) && n){
        init(); cnt = 1 ;
        map<string ,int>mp;
        string s; cin>>s;
        mp[s] = cnt ++;
        for(int i=1; i<n; i++) {
            string s1, s2; cin>>s1>>s2;
            if(!mp[s1]) mp[s1] = cnt ++;
            if(!mp[s2]) mp[s2] = cnt ++;
            ve[mp[s2]].push_back(mp[s1]);
        }
        int ans = dfs(1, -1);
        printf((check() && dp[1][0]!=dp[1][1] ? "%d Yes\n": "%d No\n" ), ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mannix_Y/article/details/84829063