poj3342 Party at Hali-Bula
树形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的更多相关文章
- 【poj3342】 Party at Hali-Bula
http://poj.org/problem?id=3342 (题目链接) 题意 给出一棵树,要求在不存在两个节点相邻的条件下,选出尽可能多的节点,并且判断是否有多种选法. Solution 很水的树 ...
- POJ3342——Party at Hali-Bula
Party at Hali-Bula Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5418 Accepted: 192 ...
- 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代表不唯一. ...
- poj 3680 Intervals
给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...
- jQuery 遍历 - parent() 方法
ylbtech-jQuery-sizzle:jQuery 遍历 - parent() 方法 parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. 1.A,jQuer ...
- (转)TCP注册端口号大全
分类: 网络与安全 cisco-sccp 2000/tcp Cisco SCCPcisco-sccp 2000/udp Cisco SCCp# Dan Wing <dwing&cisco ...
- 【转】CString类型互转 int
CString类型互转 int 原文网址:http://www.cnitblog.com/Hali/archive/2009/06/25/59632.html CString类型的转换成int 将字 ...
- 阿里云ECS被攻击
今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...
- 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 ...
随机推荐
- Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...
- Java基础之写文件——将素数写入文件中(PrimesToFile)
控制台程序,计算素数.创建文件路径.写文件. import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import ...
- SQL null值 查询null
select * from emp;
- sdutoj 2603 Rescue The Princess
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...
- JavaScript: basis
ref: http://www.imooc.com/code/387 1. html里直接嵌入js: <!DOCTYPE HTML> <html> <head> & ...
- Appium的理念
1.Appium的架构:C/S模式 Appium的核心是暴漏REST API的WebServer,appium接收来自客户端的连接请求,监听由客户端发起的命令,在移动设备上执行这些命令,这些命令的执行 ...
- Repeater 时间格式化
Repeater 时间格式化 <%# Eval("AboutDate","{0:yyyy-MM-dd hh:mm:ss}")%> 个人认为最好用 ...
- Oracle数据类型总结
一 字符串类型 1.1:CHAR类型 CHAR(size [BYTE | CHAR]) CHAR类型,定长字符串,会用空格填充来达到其最大长度.非NULL的CHAR(12)总是包含12字节信息.CHA ...
- JSP-01-搭建Web应用环境
一.搭建Web应用环境 Tomcat 下载会有两种版本,安装版和解压版,这里以解压版为例 Web服务: 是实现“基于Web无缝集成”的目标而提出的全新概念,希望通过Web服务能够实现不同的系统之间的相 ...
- python中遇到的各种问题
一 编码问题 python的默认编码是ascii码,可以修改为utf-8 在python\Lib\site-packages\下添加一个文件sitecustomize.py 内容是 import sy ...