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的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
随机推荐
- 实用and常用shell命令汇编
很久没写blog了,基本都在用 github和笔记.现在将一些常用的shell并且很使用的shell用法分享一下: 分行读取,切割,计数: cat product.txt | while read l ...
- Android开发——Snackbar使用详解
http://blog.csdn.net/qq_19431333/article/details/52862348
- C++学习笔记(二)之数组
数组作形参时,实际传入数组首地址 void print(const int*) void print(const int []) ]) 三种方法等价
- python自动化--语言基础线程、生产者消费者示例
进程与线程的区别:进程不共享空间,线程共享地址空间 线程共享空间优缺点:优点:多线程给用户的体验好些,打开时占用的内存比进程少缺点:共享地址空间会相互干扰,甚至有影响 import threading ...
- bower——基本使用
基本概念 bower可以解决项目的静态文件依赖的问题 bower是用nodejs开发的,所以要现状nodejs 安装nodejs应用程序,网上自行下载 检验是否成功安装,打开电脑cmd,执行node ...
- MySql学习笔记(二) —— 正则表达式的使用
前面介绍利用一些关键字搭配相应的SQL语句进行数据库查找过滤,但随着过滤条件的复杂性的增加,where 子句本身的复杂性也会增加.这时我们就可以利用正则表达式来进行匹配查找. 1.基本字符匹配 ' o ...
- CAD得到指定条件的实体
主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...
- 2018最新Python小白入门教程,30天学会Python
随着Python的技术的流行,Python在为人们带来工作与生活上带来了很多的便捷,因为Python简单,学起来快,也是不少新手程序员入门的首选语言.作为一名Python爱好者,我也想跟大家分享分享我 ...
- java基础学习日志--String、StringBuffer方法案例
package StringDemo; import java.util.Arrays; /* * 常用String.StringBufer类的方法 */ public class Demo1 { p ...
- nginx代理标准配置
#nginx开启的进程数worker_processes 4; #4核CPU #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log ...