HDU 搜索练习 The magic apple tree

The magic apple tree

Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 2

Problem Description

Sailormoon girls all like eating many kinds of fruit, such as banana, grape, apple and so on.<br>One day, when they was walking on a orchard, they found a magic apple tree.The magic apple tree have many nodes,but there is only one root. Each notes has its label. It is labeled from 1.On the first day,only each leaf nodes(has no children nodes) have apples. Any other nodes have no apples. The number of apples that each leaf nodes have is just the label of this node.When all the immediate children of a node each has apples,this node will grow some apple on the next day. If a node has K immediate children node,the number of apple that this node grow on next day is just the number of apples that the (K + 1) / 2th smaller node has.The Xth smaller node means there are X – 1 nodes’ number of apples is less than this node’s number of apple.<br>Now you task is to calculate the number of apples that the root has at last.

Input

There are multiple test cases.<br>Each case
contains a positive integer N, it means this tree has N nodes, labeled 1, 2, ...
N(0 < N <= 20000).<br>The following N lines describe the children of
all nodes in order of their labels. The (X + 1)th line in each test case starts
with a number p (0 <= p <N), it means the Xth node has p immediate
children nodes.then followed by p positive integer, means the label of immediate
child node

Output

Print the number of apples that the root grow at
last.

Sample Input

7

2 2 3

2 5 4

2 6 7

0

0

0

0

12

3 2 3 4

0

2 5 6

3 7 8 9

3 10 11 12

0

0

0

0

0

0

0

Sample Output

4

6

简单题意:

  树上有N个节点,每个节点又有子节点,第一天叶节点结果子,数目等于编号,第二天,其他非叶节点才开始结果子, 对于非叶节点,假设有K个子节点,那么他接的果子为子节点第(K + 1) / 2大的数,求根节点。。

思路分析:

  一开始题目都很难理解,后来就是一直超时,,,,用了输入外挂后就可以了。。。dfs搜索。。

  

# include <iostream>
# include <fstream>
# include <cstring>
# include <cstdio>
# include <algorithm>
# include <vector>
using namespace std;
vector <int> map[20001];
int is[20001], father[20001];

int dfs(int);
char c;//网上查的输入外挂
inline void f(int & x)
{
    while(c = getchar(), c < ‘0‘ || c > ‘9‘);
    x = c - ‘0‘;
    while(c = getchar(), c >= ‘0‘ && c <= ‘9‘)
    x = x * 10 + c - ‘0‘;
}
int main()
{
    //freopen("aaa.txt", "r", stdin);
    int n, t;
    while(scanf("%d", &n) == 1)
    {
        memset(is, 0, sizeof(is));
        for(int i = 1; i <= n; i++)
            map[i].clear();
        for(int i = 1; i <= n; i++)
        {
            f(father[i]); // 直接子节点的父节点
            for(int j = 0; j < father[i]; j++)
            {
                f(t);
                map[i].push_back(t);
                is[t] = 1; // 判断根节点,
            }
        }
        int root;
        for(int i = 1; i <= n; i++ )
        if(is[i] == 0)
        root = i;

        printf("%d\n", dfs(root));
    }

    return 0;
}
int dfs(int root)
{
    vector <int> tep;
    if(father[root] == 0) // 当子节点为0时, 直接返回号码
    return root;

    for(int i = 0; i < father[root]; i++)
    tep.push_back(dfs(map[root][i]));

    sort(tep.begin(), tep.end());
    return tep[(father[root] + 1) / 2 - 1];
}
时间: 2024-10-08 12:00:44

HDU 搜索练习 The magic apple tree的相关文章

杭电OJ_hdu3290_The magic apple tree

题目大意:给出一个有N(0<N<=20000)个节点的苹果树,这个树只有1个root(根节点),每个节点都有1个label(编号),label从1开始一直到N.苹果树的每个节点按照如下规则生长苹果: 1.叶子节点生长出的苹果数量等于叶子节点的label. 2.某父亲节点有K个儿子节点,直到它的K个儿子节点都生长出苹果,父亲节点才开始生长苹果.父亲节点长出的苹果数量等于它的  所有儿子中苹果数量第(k+1)/2小的  儿子节点的苹果数量. 求根节点生长出的苹果数量. 举个栗子:如下图所示,lab

HDU 4925 Apple Tree(推理)

HDU 4925 Apple Tree 题目链接 题意:给一个m*n矩阵种树,每个位置可以选择种树或者施肥,如果种上去的位置就不能施肥,如果施肥则能让周围果树产量乘2,问最大收益 思路:推理得到肯定是果树和肥料交叉种好,类似国际象棋棋盘,黑的种,白的施肥,由于格子数不多,直接去枚举每个位置即可.如果题目格子数多的话,其实也可以推出公式一步得到答案 代码: #include <cstdio> #include <cstring> const int d[4][2] = {{0, 1}

HDU 4925 Apple Tree (瞎搞)

找到规律,各一个种一棵树,或者施肥.先施肥,先种树一样. Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 197    Accepted Submission(s): 135 Problem Description I've bought an orchard and decide to plant some

hdu 4925 Apple Tree

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 思路:直接计算坐标和,如果为奇数就种树,但要注意行或列为1的情况. 写啦两种代码:一种直接判断计算的,另一种优化计算的 code1: #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> using namespace std; int main() { int T;

HDU 4925 Apple Tree 找呀找规律

间隔着取_(:зゝ∠)_ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; int n, m; int init(int i, int j) { int cnt = 1; if(i-1 >= 1) cnt *= 2; if(i+1 <= n) cnt *= 2; if(j-1 >= 1) cnt *=

2014多校第六场 1005 || HDU 4925 Apple Tree

题目链接 题意 : 给你一块n×m的矩阵,每一个格子可以施肥或者是种苹果,种一颗苹果可以得到一个苹果,但是如果你在一个格子上施了肥,那么所有与该格子相邻(指上下左右)的有苹果树的地方最后得到的苹果是两倍,如果(i,j)有一颗苹果树,(i-1,j)与(i,j+1)施了肥,那么苹果应该是1的两倍2,2的两倍4,最后是4个苹果,问你怎么安排苹果和施肥的格子使最后得到的苹果最多. 思路 : 画了图就可以看出来,苹果和苹果,肥与肥之间不要相邻就好了,所有的苹果之间都有施肥,所有施肥的格子都被苹果隔开了才能

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0

hdu 4925 Apple Tree(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4925 尽量让每棵苹果树周围都施肥,每次找到一个空地种上苹果树之后,使其周围的空地施肥,不再种苹果树. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector>

cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rooted tree with n vertices. In each leaf vertex there's a single