【Link】:https://cn.vjudge.net/contest/170078#problem/M

【Description】



求一个树的最大独立子集;

(即树的一个点集,这个点集中任意两个点,都不相连)

然后,问你这个点集是不是唯一的;

【Solution】



设d[i][0],f[i][0],表示以i为根节点的子树在i节点不选的时候的最大独立子集,f[i][0],表示在不选i的时候,这个最大独立子集是否唯一;

d[i][1],f[i][1]的定义类似,只不过变成了i这个点不选;

对于第i个节点要选的情况;

儿子节点只能都不选

d[i][0]=∑d[j][1]

(j为i的儿子,当f[j][1]全部为true,f[i][0]才为true)

对于第i个节点不选的情况

儿子节点可选、可不选

d[i][1]=∑max(d[j][0],d[j][1])

这里,如果d[j][0]==d[j][1],或者d[j][0]和d[j][1]中那个较大值的f值为false;

则f[i][1]为false;否则为true;

最后,在根节点1上面加一个0节点

直接输出d[0][1];

->因为0节点不选的话,对应1节点可选可不选;

然后根据f[0][1]判断YES和NO;



【NumberOf WA】



1



【Reviw】



d[y][0]写成了dy[0]…

然后dy是那个模板里的四个方向数组….



【Code】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 210; int n,cnt;
string boss;
map <string,int> dic;
vector <int> g[N];
int d[N][2];
bool f[N][2]; void dfs(int x){
d[x][0] = 1,f[x][0] = true;
d[x][1] = 0,f[x][1] = true;
int len = g[x].size();
rep1(i,0,len-1){
int y = g[x][i];
dfs(y);
d[x][0]+=d[y][1],f[x][0] = f[x][0] && f[y][1];
if (d[y][0]==d[y][1]) f[x][1] = false,d[x][1] += d[y][1];
if (d[y][0] > d[y][1]){
d[x][1] += d[y][0];
if (!f[y][0]) f[x][1] = false;
}
if (d[y][0] < d[y][1]){
d[x][1] += d[y][1];
if (!f[y][1]) f[x][1] = false;
}
}
} int main(){
//Open();
Close();
while (cin >> n && n){
rep1(i,0,n) g[i].clear();
cnt = 0;
dic.clear();
cin >> boss;
dic[boss] = ++cnt;
string x,y;
rep1(i,1,n-1){
cin >> x >> y;
if (dic[x]==0) dic[x]=++cnt;
if (dic[y]==0) dic[y]=++cnt;
int xx = dic[x],yy = dic[y];
g[yy].pb(xx);
}
g[0].pb(1);
dfs(0);
cout << d[0][1] <<' ';
if (f[0][1])
cout <<"Yes"<<endl;
else
cout <<"No"<<endl;
}
return 0;
}

【Uva 1220】Party at Hali-Bula的更多相关文章

  1. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

  9. 【Uva 11400】Lighting System Design

    [Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...

随机推荐

  1. 2019 牛客多校第一场 C Euclidean Distance ?

    题目链接:https://ac.nowcoder.com/acm/contest/881/C 题目大意 给定 m 和 n 个整数 ai,$-m \leq a_i \leq m$,求$\sum\limi ...

  2. java 读取mysql中数据 并取出

    public static String url = null; public static String username = null; public static String password ...

  3. 火狐不支持webp格式的图片

    <!DOCTYPE html> <html lang="en"> <style> ul{list-style: none;} li{float: ...

  4. 6-Python操作MySQL-增(insert)-删(delete)-改(update)-查(select)

    增删改 from pymysql import * def main(): # 创建Connection连接 conn = connect(host='localhost',port=3306,dat ...

  5. Eclipse指定JDK版本

    Eclipse有好多版本,同时又分32位和64位,要使用相对应的版本和一样位数的JDK,Eclipse才能正常运行. 对应不上时,Eclipse 甚至不能正常启动.报错:“Failed to load ...

  6. Activiti学习笔记目录

    1.Activiti学习笔记1 — 下载与开发环境的配置: 2.Activiti学习笔记2 — HelloWorld: 3.Activiti学习笔记3 — 流程定义: 4.Activiti学习笔记4 ...

  7. iOS开发系列-iOS适配

    概述 发布iPhone X 系统版本为iOS11, 由于刘海屏原因需要对新的机型做适配. iPhone X safeArea iOS11苹果提出safeArea替代iOS7引入 topLayoutGu ...

  8. CSS——元素的显示与隐藏

    元素的显示与隐藏 在CSS中有三个显示和隐藏的单词比较常见,我们要区分开,他们分别是 display visibility 和 overflow. 他们的主要目的是让一个元素在页面中消失,但是不在文档 ...

  9. Arcmap中处理面图层中空白碎片

    在面数据中,有时候在一个面要素中会出现碎片,而我们大多时候不希望这些碎片存在(图1),下面介绍通过Editor工具把这些碎片处理掉.

  10. 搭建CA颁发证书做https加密网站

    92.168.10.187 CA服务器 192.168.10.190 web服务器 (1)搭建CA cd /etc/pki/CA 在这个目录下创建serial和index.txt两个文件 echo 0 ...