树形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. Dive into python 实例学python (2) —— 自省,apihelper

    apihelper.py def info(object, spacing=10, collapse=1): """Print methods and doc strin ...

  2. iOS8中用UIVisualEffectView实现高斯模糊视图(毛玻璃效果)

    UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *vi ...

  3. SQL Server 身份验证 登陆

    当遇到错误 检查SQL是否启动 SQL Server (MSSQLSERVER)在 打开 SQL Server 配置管理器 SQL Server (MSSQLSERVER) 鼠标右键->启动 再 ...

  4. 转:MIME(Multipurpose Internet Mail Extensions)类型

    MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器 ...

  5. spark sql cache

    1.几种缓存数据的方法 例如有一张hive表叫做activity 1.CACHE TABLE //缓存全表 sqlContext.sql("CACHE TABLE activity" ...

  6. paper 3:matlab中save,load使用方法小结

    功能描述]存储文件[软件界面]MATLAB->File->Save Workspace As将变量存入硬盘中指定路径.[函数用法] save:该函数将所有workspace中变量用二进制格 ...

  7. PAT乙级 1023. 组个最小数 (20)

    1023. 组个最小数 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定数字0-9各若干个.你可以以 ...

  8. [php] How to debug PHP in the terminal

    Here I use Netbeans, xdebug to debug the PHP in the terminal of Ubuntu. 1. you have to install the x ...

  9. [ubuntu] ubuntu13.04安装rabbitcvs管理svn

    加入源 sudo add-apt-repository ppa:rabbitvcs/ppa 更新 sudo apt-get update 安装软件 sudo apt-get install rabbi ...

  10. 安装好android的adt以后重启eclipse,但是没有创建AVD的图标

    安装好android的adt以后重启eclipse,但是没有创建AVD的图标: 解决方法: 1. 先检查ADT是否已经安卓成功 2. Windows--- Customize Perspective ...