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 ...
随机推荐
- 学习OpenCV——鼠标事件(画框)
#include "cv.h" #include "highgui.h" bool check_line_state=false; IplImage* work ...
- UVa 10088 - Trees on My Island (pick定理)
样例: 输入:123 16 39 28 49 69 98 96 55 84 43 51 3121000 10002000 10004000 20006000 10008000 30008000 800 ...
- 【Origin】 画道
-夏之时,晚归途中,观云,感雨,识迷障,题以记之. 原道是东边日出西边雨, 却原来乌云蔽日雨不息; 只不过光芒也破云边洒, 可还是满地泥泞水花起. --作于二零一六年七月十一日
- Java SE series:2. enhance your java basis! [doc chm: jdk6api Chinese reference]
1. javaee(Web) and Android 2. how to use eclipse and break point debuging in eclipse, as to java web ...
- Undefined index:
$username=$_POST["username"]; 初学时发现这个错误, Undefined index: username 如果不仔细看,还以为是$username报错, ...
- 通过struts.xml搭建、为属性注入值_2015.01.04
01:web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app version=& ...
- spark streaming - kafka updateStateByKey 统计用户消费金额
场景 餐厅老板想要统计每个用户来他的店里总共消费了多少金额,我们可以使用updateStateByKey来实现 从kafka接收用户消费json数据,统计每分钟用户的消费情况,并且统计所有时间所有用户 ...
- angular Creating a Directive that Adds Event Listeners
<span my-draggable>Drag ME</span> angular.module('dragModule', []) .directive('myDraggab ...
- js 实现精确加减乘除
//加法 function accAdd(arg1, arg2) { var r1, r2, m; try { r1 = arg1.toString().split(".")[1] ...
- 让未激活的win8.1不再跳出提示激活的窗口
以管理员运行命令行: 输入以下命令: slmgr.vbs -upk