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

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

Source

Tehran 2006



题目链接:http://poj.org/problem?id=3342



题目大意:一棵树,父亲和儿子不能同一时候选入同一个集合,如今求能选集合中元素个数最多的那个集合大小。并推断解是否唯一



题目分析:求树的最大独立集的问题。好题,也用了两次dp的思想,有点类似HDU 5282这题的思想

dp[i][0]表示不选第i个结点,集合大小的最大值

dp[i][1]表示选第i个结点,集合大小的最大值

对于此dp显然

dp[i][1] = dp[son][0]  选父亲则不能选儿子

dp[i][0] = max(dp[son][0], dp[son][1]) 不选父亲的话则值等于选儿子或者不选儿子里的较大值



s[i][1] == true 表示选第i个结点时有唯一解,false表示解不唯一

s[i][0] == true 表示不选第i个结点时有唯一解。false表示解不唯一

開始时设s[i][1],s[i][0]都为true,对于此dp。我们主要考虑父亲的解变成不唯一的情况

与第一个dp状态相应,分成选父亲和不选父亲两种情况

if(!s[son][0])  s[i][1] = false,意思是假设不选儿子时有多个解,则此时能够选父亲,选父亲也肯定有多个解

if(dp[son][0] == dp[son][1])  s[i][0] = false。假设选不选儿子的答案同样。显然不选父亲时有多个解,由于选不选儿子都能够



最后自叶子向根回溯求解推断就可以。这题由于map没清零,wa了大半天。

。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
int const MAX = 300;
int n;
int dp[MAX][2];
bool s[MAX][2];
bool vis[MAX];
vector <int> vt[MAX]; void DFS(int fa)
{
dp[fa][1] = 1;
vis[fa] = true;
s[fa][0] = true;
s[fa][1] = true;
int sz = vt[fa].size();
for(int i = 0; i < sz; i++)
{
int son = vt[fa][i];
if(!vis[son])
{
DFS(son);
dp[fa][1] += dp[son][0];
dp[fa][0] += max(dp[son][0], dp[son][1]);
if(dp[son][0] == dp[son][1])
s[fa][0] = false;
if(!s[son][0])
s[fa][1] = false;
}
}
return;
} int main()
{
while(scanf("%d", &n) != EOF && n)
{
map <string, int> mp;
for(int i = 0; i < MAX; i++)
vt[i].clear();
memset(vis, false, sizeof(vis));
memset(dp, 0, sizeof(dp));
int cnt = 0;
string boss, fir, sec;
cin >> boss;
mp[boss] = cnt ++;
for(int i = 0; i < n - 1; i++)
{
cin >> sec >> fir;
if(!mp.count(fir))
mp[fir] = cnt ++;
if(!mp.count(sec))
mp[sec] = cnt ++;
vt[mp[fir]].push_back(mp[sec]);
}
DFS(0);
if(dp[0][1] > dp[0][0] && s[0][1])
printf("%d Yes\n", dp[0][1]);
else if(dp[0][1] < dp[0][0] && s[0][0])
printf("%d Yes\n", dp[0][0]);
else
printf("%d No\n", max(dp[0][0] , dp[0][1]));
}
}

POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)的更多相关文章

  1. POJ3398Perfect Service[树形DP 树的最大独立集变形]

    Perfect Service Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1518   Accepted: 733 De ...

  2. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  3. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  4. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  5. POJ 3162 bit区间查询最值+树形DP

    POJ 3162 『题目链接』POJ 3162 『题目类型』bit区间查询最值+树形DP ✡Problem: 一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远 ...

  6. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  7. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

  8. poj 2342 【Anniversary party】树形dp

    题目传送门//res tp poj 题意 给出一棵有权树,求一个节点集的权值和,满足集合内的任意两点不存在边 分析 每个点有选中与不选中两种状态,对于第\(i\)个点,记选中为\(sel_i\),不选 ...

  9. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. 【JavaScript从入门到精通】第二课

    第二课 初探JavaScript魅力-02 变量 说起变量,我们不得不提起我们有一部比较古老的电视剧叫<包青天>.包青天有一把非常厉害的宝剑叫“尚方宝剑”,见到尚方宝剑有如见到皇帝.某种程 ...

  2. Learning Discriminative and Transformation Covariant Local Feature Detectors实验环境搭建详细过程

    依赖项: Python 3.4.3 tensorflow>1.0.0, tqdm, cv2, exifread, skimage, glob 1.安装tensorflow:https://www ...

  3. C++ typedef用法小结 (转载)

    声明:本文转自charley_yang,点击此处查看原文 第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // ...

  4. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. JavaScript编程那些事(牛客网 LeetCode)

    计算给定数组 arr 中所有元素的总和 本人提供常规方法 function sum(arr) { var len = arr.length; var sum = 0; if(len == 0){ su ...

  6. 【02】emmet系列之HTML语法

    [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 初始化 :快速编写HTML ...

  7. Fiddler简介与Web抓包,远程抓包(IE、360、谷歌、火狐)

    Fiddler简介以及web抓包 一.Fiddler简介 简单来说,Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯.网上简介很多,我们不多说. 二 ...

  8. hdu 1251简单字典树

    #include<stdio.h> #include<iostream> #include<string.h> using namespace std; struc ...

  9. 序列终结者(bzoj 1521)

    Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...

  10. Linux下汇编语言学习笔记23 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...