http://poj.org/problem?id=1694 (题目链接)

题意

  一棵树,现在往上面放石子。对于一个节点x,只有当它的直接儿子都放满石子时,才能将它直接儿子中的一个石子放置x上,并回收其它石子。问最少需要多少个石子才能将石子放到根节点上。

Solution

  显然,贪心。数据范围小的可怜,用f[x]表示将石子放到节点x需要的最小石子数。然后对于一个节点的儿子节点,先放f[x]大的,再放小的。

细节

  多组数据清空数组

代码

// poj1694
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define inf 2147483640
#define MOD 1000000007
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=1000;
struct edge {int to,next;}e[maxn];
int head[maxn],f[maxn],n,cnt;
vector<int> v[maxn]; bool cmp(int a,int b) {
return a>b;
}
void link(int u,int v) {
e[++cnt]=(edge){v,head[u]};head[u]=cnt;
e[++cnt]=(edge){u,head[v]};head[v]=cnt;
}
void dfs(int x,int fa) {
f[x]=1;
for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) {
dfs(e[i].to,x);
v[x].push_back(f[e[i].to]);
}
sort(v[x].begin(),v[x].end(),cmp);
for (int i=0;i<v[x].size();i++) f[x]=max(f[x],v[x][i]+i);
}
void Init() {
for (int i=1;i<=n;i++) head[i]=0,v[i].clear();
cnt=0;
}
int main() {
int T;scanf("%d",&T);
while (T--) {
Init();
scanf("%d",&n);
for (int u,v,son,i=1;i<=n;i++) {
scanf("%d%d",&u,&son);
for (int j=1;j<=son;j++) {
scanf("%d",&v);
link(u,v);
}
}
dfs(1,0);
printf("%d\n",f[1]);
}
return 0;
}

  

【poj1694】 An Old Stone Game的更多相关文章

  1. 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)

    [题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...

  2. 【POJ】A New Stone Game(博弈论)

    http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...

  3. 【Leetcode_easy】1046. Last Stone Weight

    problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完

  4. 【leetcode】1046. Last Stone Weight

    题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...

  5. 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...

  6. 【poj1738】 An old Stone Game

    http://poj.org/problem?id=1738 (题目链接) 题意 一排n堆石子,合并两堆石子的代价为两堆石子总数之和.问将所有石子合并为一堆所需要的最小代价. Solution 本来想 ...

  7. 【poj1740】 A New Stone Game

    http://poj.org/problem?id=1740 (题目链接) 男人八题之一 题意 对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该 ...

  8. 【计算几何初步:多边形中心】【HDU1115】Lifting the Stone

    一.质点系重心公式 x=(x1*m1+x2*m2+x3*m3.....xn*mn)/M  (M=m1+m2+m3+m4...+mn) 二.三角形重心 可直接求得,但在多边形剖分中 各三角形的质点的质量 ...

  9. 【翻译】A simple stone game

    题目描述 在学会了如何玩尼姆游戏之后,迈克开始尝试另一种看起来很多的石头游戏. 简单点. 游戏是这样的:两个玩家用一堆n块石头开始游戏.他们轮流从堆里取石头,每次至少取一块石头 一堆又一堆,每次他们至 ...

随机推荐

  1. RubyMine不能调试Rails项目的问题

    需要安装debase gem,而且在项目的GemFile中禁用byebug

  2. OracleHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  3. ORACLE临时表总结

    临时表概念 临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别.它只能存储在临时表空间,而非用户的表空间.ORACLE临时表是会话或事务级别的,只对 ...

  4. 记一道有意思的算法题Rotate Image(旋转图像)

    题出自https://leetcode.com/problems/rotate-image/ 内容为: You are given an n x n 2D matrix representing an ...

  5. Oracle 12.1.0.2 New Feature翻译学习【In-Memory column store内存列存储】【原创】

    翻译没有追求信达雅,不是为了学英语翻译,是为了快速了解新特性,如有语义理解错误可以指正.欢迎加微信12735770或QQ12735770探讨oracle技术问题:) In-Memory Column ...

  6. OAF messageChoice 关联问题

    最近有个需求,就是采购订单的供应商要按照一级和二级来选,一级关联二级,二级关联供应商.之前的一级和二级都是用LovInput做的,现在想要改为messageChoice.如下图: 改为: 下面给大家介 ...

  7. java int与integer的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...

  8. Ubuntu nginx 配置404错误页面

    1.创建自己的404.html页面: 2.更改nginx.conf在http定义区域加入: /etc/nginx# vim nginx.conf 下添加 fastcgi_intercept_error ...

  9. 前端构建工具gulp使用

    前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中文文档 https://g ...

  10. Python版本2.7切3.5和3.5切2.7

    在Ubuntu上是自带Python2.7和3.5的 当你在终端输入Python的时候是显示Python2.7的 叫大家你怎么切换到Python3.5版本 1,查看是否存在python3.5终端输入 c ...