题目链接

  • 题意:

    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. Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)

    Oracle 安装过程中,先决条件检查遇到如下错误: 正在检查网络配置要求...  检查完成.此次检查的总体结果为: 失败 <<<<  问题: 安装检测到系统的主 IP 地址是 ...

  2. C#基础 集合

    //数组定义的时候 //需要定义数据类型 //需要定义初始长度 //int [] array = new int[5]; //int a = array.Length; //集合 //ArrayLis ...

  3. Disruptor源码解读

    上一篇已经介绍了Disruptor是什么?简单总结了为什么这么快?下面我们直接源码搞起来,简单粗暴.高性能队列disruptor为什么这么快? 一.核心类接口 Disruptor 提供了对RingBu ...

  4. 完成fcc作业2时思路

    1.设置导航链接按钮栏时,不能用文档流,要用position:fixed;固定在窗口上方, 其他普通流盒子按上下顺序就用position:relative:后面发现导航栏被普通流盒子挡在了下面,就设置 ...

  5. 【转】jvm类加载

    类加载机制 JVM把class文件加载的内存,并对数据进行校验.转换解析和初始化,最终形成JVM可以直接使用的Java类型的过程就是加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的生命 ...

  6. windows2008 rs+sql 2008 下配置站点权限

    几点注意 Windows 2008 iis7.5  1 建立应用程序池 2 文件目录的权限加上 IIS AppPool\应用程序池名称 (找不到直接录入) 3 文件目录要给 IUser权限,不然出错. ...

  7. Windows10环境中 laravel任务调度 如何启动调度

    Windows10环境中 laravel任务调度 如何启动调度 一:问题由来 1:今天在做用laravel开发订单系统的时候,需要使用定时任务来大批量提交订单,测试一下订单金额是否有误.发现larav ...

  8. HTML 5 <aside> 标签

    定义和用法 <aside> 标签定义 article 以外的内容.aside 的内容应该与 article 的内容相关. 实例 <p>Me and my family visi ...

  9. Leetcode747至少是其他数字两倍的最大数

    Leetcode747至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 .查找数组中的最大元素是否至少是数组中每个其他数字的两倍.如果是,则返回最大元素的索引,否则返回-1 ...

  10. 洛谷——P1273 有线电视网

    P1273 有线电视网 题目大意: 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树 ...