题目链接

  • 题意:

    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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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/ ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

  9. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

随机推荐

  1. 全面学习ORACLE Scheduler特性(4)创建和管理Schedule

    三.使用Schedules 10g 中新推出的SCHEDULER可能确实会让很多初接触的朋友感觉晕头晕脑,相比之前的jobs,SCHEDULER中新增的概念太多.比如说jobs,仍然可以理解成之前版本 ...

  2. MongoDB学习笔记~复杂条件拼接和正则的使用

    在大叔lind框架里有日志组件logger,而在日志实现方式上有file,mongodb,sql,json等方式,对分布式日志处理上大叔推荐使用mongodb进行存储,除了它的高效写入,灵活的结构外, ...

  3. hbase本地调试环境搭建

    1,前言 想要深入的了解hbase,看hbase源码是必须的.以下描述了搭建hbase本地调试环境的经历 2,安装步骤 2.1,启动hbase 1,安装java和IDE IntelliJ,下载源码等. ...

  4. SQL server存储过程及触发器基础

    存储过程:就像函数一样的会保存在数据库中-->可编程性 --> 存储过程-----------------------------------------------------创建存储过 ...

  5. 【Python-2.7】换行符和制表符

    在Python中换行符“\n”表示接下来的内容将会换到下一行显示,制表符“\t”表示下面的内容显示时在前面留出空白,如打印如下内容: Dear: I love you forever! 上面的一段话分 ...

  6. Digital design之Boolean Algebra

    1. 0 and 1 (duality: 0 -- 1, · -- +) X + 0 = X, X · 1 = X X + 1 = 1, X · 0 = 0 2. Idempotent X + X = ...

  7. Xamarin绑定ios静态库

    以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...

  8. java虚拟(一)--java内存区域和常量池概念

    一.java运行时数据区 也可以称为java内存区域,和java内存模型不是一回事,不要弄混,这里基于jdk1.8之前 1.1.方法区 线程共享,类装载过程中产生的java.lang.Class对象保 ...

  9. BZOJ 1058: [ZJOI2007]报表统计 multiset + 卡常

    Code: #include<bits/stdc++.h> #define maxn 600000 #define inf 1000000000 using namespace std; ...

  10. 查询条件中,不进sql语句 也不进后台bug

    前端代码:本来代码中少写了value="1",后来加上value值之后,可以正常进方法 <div class="row"> <label cl ...