题目链接

  • 题意:

    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. 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...

  2. 在dataGridView空间中添加数据

    //查询信息sql语句 string sql = "select studentName,addres from student"; SqlDataAdapter adapter ...

  3. 前端面试题HTML

    浏览器页面有哪三层构成,分别是什么,作用是什么?

  4. solr深分页,游标操作分页,解决性能问题

    solr深分页,游标操作分页,解决性能问题 @Test public void pageByCursor() { try { solrServer.connect(); String query = ...

  5. Android显示相册图片和相机拍照

    首先看最重要的MainActive类: public class MainActivity extends AppCompatActivity { private final int FROM_ALB ...

  6. PHP——基本使用(二)

    PHP与Apache Apache服务器在接受到客户端请求的时候,根据客户端所请求的文件的类型,然后去问模块能否处理此文件,php作为模块之一有可能可以处理此文件,处理之后将数据再返回给apache, ...

  7. Java中PrintStream(打印输出流)

    Java中PrintStream(打印输出流)   PrintStream 是打印输出流,它继承于FilterOutputStream. PrintStream 是用来装饰其它输出流.它能为其他输出流 ...

  8. HDU_1175_连连看

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大意:连连看规则,只能转两次弯,先输入矩阵0表示没有棋子,正整数表示不同的棋子,然后询问,输入两点坐 ...

  9. iOS,Core Animation--负责视图的复合功能

    简介 UIKit API UIKit是一组Objective-C API,为线条图形.Quartz图像和颜色操作提供Objective-C 封装,并提供2D绘制.图像处理及用户接口级别的动画.    ...

  10. 国外一些好用的UX/UI设计工具和资源介绍

    你今天使用的设计工具也许不再适合以后的网页和APP设计项目了.新的工具不断的推出市场,目标只有一个,让你的工作更快.更容易而且工作成效更好.以下就是各种工具的介绍入口,当您点击标题就会看到各种很好的工 ...