Codeforces Round #245 (Div. 1)——Guess the Tree
- 题意:
n个节点,给定每一个节点的子树(包含自己)的节点个数。每一个节点假设有子节点必定大于等于2。求这种数是否存在
n (1 ≤ n ≤ 24). - 分析:
- 用类似DP的思路,从已知開始。这题的已知显然是叶子,那么从叶子開始考虑。
如今给一个节点,子树节点数为x。那么从叶子中找x-1个就可以。之后再来一个y。不放设y <= x,这时候就有两种选择,尽量选1或者尽量选x。分析一下:首先明确一点。无论怎样选择,之后能用的点的和是一定的。假设尽量选小的,那么会使得选过之后的点数小的比較少。假设尽量选大的,那么之后的点数的方差比較大(表述比較抽象。事实上就是大的更大。小的还在),那么显然尽量选大的是最好的
后来发现有些瑕疵,尽量选大的可能会导致当前点无解,比方1 1 1 1 1 1 1 4 3 3 7 12这个数据
所以在选择的时候,贪心选择,假设无解再回溯就可以
const int MAXN = 50; struct Node
{
int x;
Node (int x) : x(x) {}
bool operator< (const Node& rhs) const
{
return x > rhs.x;
}
};
int ipt[MAXN];
map<Node, int>mp;
map<Node, int>::iterator it; bool dfs(map<Node, int>::iterator it, int& v)
{
if (it == mp.end())
{
if (v == 0) return true;
return false;
}
int n = it->first.x;
int num = min(v / n, it->second);
FED(i, num, 0)
{
it->second -= i;
v -= i * n;
if (dfs(++it, v))
return true;
v += i * n;
it--;
it->second += i;
}
return false;
} int main()
{
// freopen("in.txt", "r", stdin);
int n;
while (~RI(n))
{
mp.clear();
bool ok = true;
int cnt = 0;
REP(i, n)
{
int t;
RI(t);
if (t == 1) mp[Node(t)]++;
else ipt[cnt++] = t;
}
sort(ipt, ipt + cnt);
REP(i, cnt)
{
int t = ipt[i] - 1;
it = mp.upper_bound(Node(t));
if (!dfs(it, t))
{
ok = false;
break;
}
it = mp.begin();
while (it != mp.end())
{
if (it->second == 0)
mp.erase(it++);
else
it++;
}
mp[Node(ipt[i])]++;
}
if (mp.size() != 1 || mp.begin()->second != 1) ok = false;
if (ok) puts("YES");
else puts("NO");
}
return 0;
}
Codeforces Round #245 (Div. 1)——Guess the Tree的更多相关文章
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
D. Happy Tree Party Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1)D. Water Tree dfs序
D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
C. Xor-tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
随机推荐
- Docker (1) 基本概念和安装
Docker简介 什么是容器? 一种虚拟化的方案,操作系统级别的虚拟化.容器是一个轻量的.独立的.可执行的包,包含了执行它所需要的所有东西:代码.运行环境.系统工具.系统库.设置.很长一段时间中,容器 ...
- rem自适应布局小结001
在最近的移动端布局当中,最炙手可热的方式便是使用rem进行元素的布局.以下便是从最近的文章中所总结出来的一点东西. 首先,我们必须有以下的疑问: rem的本质是什么? rem如何实现自适应布局? 如何 ...
- arduino相关资料和网站
2018-02-0212:59:12 昨天晚上在论坛里看了大半夜,找到了很多有意思的项目,总结一下! http://www.51hei.com/bbs/dpj-105654-1.html ---贪 ...
- 面试中的一些小问题之ES5和ES6的区别?
1995年,JavaScript作为网景浏览器的一部分首次发布,起初并不叫JavaScript,而是叫LiveScript,但是因为当时Java正火,也算是为了搭上java的顺风车,于是改成了Java ...
- 如何利用sql注入进行爆库
SQL注入能做什么 在<SQL注入基础>一文介绍了SQL注入的基本原理和实验方法,那接下来就要问一下,SQL注入到底能什么? 估计很多朋友会这样认为:利用SQL注入最多只能获取当前表中的所 ...
- TCP/IP和UDP的比较
TCP.UDP详解 1.传输层存在的必要性 由于网络层的分组传输是不可靠的,无法了解数据到达终点的时间,无法了解数据未达终点的状态.因此有必要增强网络层提供服务的服务质量. 2.引入传输层的原因 面向 ...
- php redis 操作大全
类和方法 用法 Redis类 类RedisException 预定义的常量 Redis类 说明:创建一个Redis客户端 例 $redis = new Redis(); 类RedisException ...
- 解决:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
今天发布web API,调用接口报错了:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31b ...
- U盘安装完美的WIN7操作系统教程
准备工作 首先备份或者在官网下载好您机器的驱动,否则完成后可能无法正常使用 ①一个有win7或者XP系统的电脑(制作启动盘用) ②一个4G以上的U盘 ③win7&win8系统包(请到官网下载或 ...
- Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout
automaticallyAdjustsScrollViewInsets:在导航栏透明时用到 In your viewDidLoad method, add if([self respondsToSe ...