【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. PAT_A1106#Lowest Price in Supply Chain

    Source: PAT A1106 Lowest Price in Supply Chain (25 分) Description: A supply chain is a network of re ...

  2. PAT_A1043#Is It a Binary Search Tree

    Source: PAT A1043 Is It a Binary Search Tree (25 分) Description: A Binary Search Tree (BST) is recur ...

  3. Spring 基于xml配置方式的事务(14)

    参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...

  4. springboot中参数校验

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  5. JDBC_数据库连接池工具类

    //定义一个类JDBCUtils public class JDBCUtils { //1.定义成员方法 private static DataSource ds; //2.提供静态代码块 stati ...

  6. java_MySQL未整理

    package cn.aikang.MySql; public class MySqlTest {/*数据库的基本概念: 1.数据库的英文单词:DataBase简称:DB 2.什么是数据库:用于存储和 ...

  7. java OOP第03章_继承、抽象类和抽象方法

    一. 为什么需要继承: 若多个类中都需要一些属性和方法,那么就可以将属性和方法抽取到一个父类中,需要的子类可以通过extends关键字去继承这个父类后拥有相应的属性和方法. 类.数组.方法----引用 ...

  8. CF421D Bug in Code

    题意:n个人每人选择了另外不相同的两个人.问有多少对(x,y)使得这n个人中至少有p个选择了至少其中之一? 标程:那就不写了吧. 题解:容斥 统计Ax表示有多少个人选择了x. 一般来说有Ax+Ay&g ...

  9. 树莓派3B+ 人脸识别、摄像头安装和使用

    最近在学校里折腾树莓派上的人脸识别,折腾了很久才能用 在此记录下使用的过程和遇到的困难 过程基于超有趣!手把手教你使用树莓派实现实时人脸检测完成的.其中前面opencv的安装是文章中的Raspbian ...

  10. 常用css初始化样式(淘宝)

    最简单粗暴的css初始化样式就是:*{padding:0:margin:0}(不推荐) 淘宝的样式初始化: body, h1, h2, h3, h4, h5, h6, hr, p, blockquot ...