树形dp题,状态转移方程应该很好推,但一定要细心。

http://poj.org/problem?id=3342

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
const int maxn = + ;
char buf[maxn];
struct Edge{
int to, next;
}edge[maxn << ];
int head[maxn], N;
map<string, int> mapi;
int n, k;
int dp[maxn][];
int o[maxn][];
int get_str(char *dest){
int cnt = ;
char ch;
do{
ch = getchar();
}while(ch != EOF && (ch == ' ' || ch == '\n'));
if(ch == EOF) return ;
dest[cnt++] = ch;
while((ch = getchar()) != ' ' && ch != '\n' && ch != EOF) dest[cnt++] = ch;
dest[cnt] = '\0';
return cnt;
} void addEdge(int u, int v){
edge[N].next = head[u];
edge[N].to = v;
head[u] = N++;
} void dfs(int u, int fa){
dp[u][] = ;
int tem1 = , tem2 = ;
for(int i = head[u]; i + ; i = edge[i].next){
int v = edge[i].to;
if(v == fa) continue;
dfs(v, u);
dp[u][] += dp[v][];
o[u][] |= o[v][];
int d = dp[v][] > dp[v][] ? : (dp[v][] == dp[v][] ? - : );
if(d == -){
dp[u][] += dp[v][];
o[u][] = ;
}else{
dp[u][] += dp[v][d];
o[u][] |= o[v][d];
}
}
} int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n) && n){
k = ;
int base = ;
get_str(buf);
mapi.clear();
mapi[string(buf)] = ++base;
N = ;
memset(head, -, sizeof head);
for(int i = , x, y; i < n; i++){
get_str(buf);
if(mapi.find(string(buf)) == mapi.end()) mapi[string(buf)] = ++base, x = base;
else x = mapi[string(buf)];
get_str(buf);
if(mapi.find(string(buf)) == mapi.end()) mapi[string(buf)] = ++base, y = base;
else y = mapi[string(buf)];
addEdge(x, y);
addEdge(y, x);
}
memset(dp, , sizeof dp);
memset(o, , sizeof o);
dfs(, );
int ans = -;
bool ok = ;
for(int i = ; i <= n; i++) for(int j = ; j <= ; j++){
if(dp[i][j] > ans) ans = dp[i][j], ok = o[i][j];
else if(dp[i][j] == ans) ok = ;
}
printf("%d %s\n", ans, ok ? "No" : "Yes");
}
return ;
}

poj3342 Party at Hali-Bula的更多相关文章

  1. 【poj3342】 Party at Hali-Bula

    http://poj.org/problem?id=3342 (题目链接) 题意 给出一棵树,要求在不存在两个节点相邻的条件下,选出尽可能多的节点,并且判断是否有多种选法. Solution 很水的树 ...

  2. POJ3342——Party at Hali-Bula

    Party at Hali-Bula Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5418   Accepted: 192 ...

  3. POJ3342 Party at Hali-Bula(树形DP)

    dp[u][0]表示不选u时在以u为根的子树中最大人数,dp[u][1]则是选了u后的最大人数: f[u][0]表示不选u时的唯一性,f[u][1]是选了u后的唯一性,值为1代表唯一,0代表不唯一. ...

  4. poj 3680 Intervals

    给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...

  5. jQuery 遍历 - parent() 方法

    ylbtech-jQuery-sizzle:jQuery 遍历 - parent() 方法  parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. 1.A,jQuer ...

  6. (转)TCP注册端口号大全

    分类: 网络与安全 cisco-sccp 2000/tcp Cisco SCCPcisco-sccp 2000/udp Cisco SCCp# Dan Wing <dwing&cisco ...

  7. 【转】CString类型互转 int

    CString类型互转 int 原文网址:http://www.cnitblog.com/Hali/archive/2009/06/25/59632.html CString类型的转换成int  将字 ...

  8. 阿里云ECS被攻击

    今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...

  9. POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)

    POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...

随机推荐

  1. spring mvc 的Controller类默认Scope是单例(singleton)的

    使用Spring MVC有一段时间了,之前一直使用Struts2,在struts2中action都是原型(prototype)的, 说是因为线程安全问题,对于Spring MVC中bean默认都是(s ...

  2. hibernate主键生成机制与save返回

    主键生成机制为assigned时,save之后通过get得不到id(主键),使用identity可以. hibernate主键生成机制1) assigned主键由外部程序负责生成,无需Hibernat ...

  3. 安装shopex注意事项

    [原创]关于PHP5.3.x和Zend Optimizer(Zend Guard Loader),以及shopex4.8.5安装的问题  http://dzmailbox.blog.163.com/b ...

  4. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

  5. Lintcode: Majority Number III

    Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...

  6. 面向对象 理解 C#复习

    面向对象: 是基于万物皆对象这个哲学观点. 所谓的面向对象就是将我们的程序模块化,对象化,把具体事物的特性属性和通过这些属性来实现一些动作的具体方法放到一个类里面 通俗点讲: 一切都是对象 举例: 将 ...

  7. Python条件循环判断

    1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: 1 2 3 4 5 6 7 8 9 age_of_cc = 27   age = int( ...

  8. [转]关于安装hadoop中出现的的 $HADOOP_HOME is deprecated 的解决方法

    当前用户的.bash_profile在/home/用户/下,系统的.bash_profile在/etc/skel目录下; 默认可能是隐藏的:有人会问了,隐藏的我怎么打开它,一个简单的办法,直接使用vi ...

  9. android中的命令安装与卸载

    软件的安装: adb install apk的保存地址 卸载软件: adb uninstall  package名

  10. 【crunch bang】程序中文化

    在应用程序中配置使用中文显示. # apt-get install locales # dpkg-reconfigure locales 安装文泉驿-微米黑字体: sudo apt-get insta ...