SDUT2013级测试赛_D

题目描述

给出一棵含有n个点的树,每个点权值为wi,求从根节点到叶子结点权值和最大的那条路经的权值和是多少。

输入

n(1<= n && n <= 10000)。

接下来n+1行,每行两个整数w(w
<= 1000)。

第i个节点的父节点为w,若 i为根节点。600组数据。

输出

对于每组数据,输出一个数代表答案。

示例输入

3
0 5
1 5
1 6

示例输出

11

提示

来源

解题报告

求从根节点出发到叶子的最长路。。。很像数塔。。。

我暴力dfs过了.怒搜所有数枝,搜完答案就有了。。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
    int v,next;
} edge[100000];
int head[10010],cnt,vv[10010],vis[10010],n,maxx;
void add(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int s,int d)
{
    int i,j;
    for(i=head[s]; i!=-1; i=edge[i].next)
        dfs(edge[i].v,d+vv[edge[i].v]);
    if(d>maxx)
        maxx=d;
}
int main()
{
    int i,j,v,w;
    while(~scanf("%d",&n))
    {
        maxx=0;
        cnt=0;
        memset(edge,0,sizeof(edge));
        memset(vv,0,sizeof(vv));
        memset(head,-1,sizeof(head));
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&v,&w);
            add(v-1,i);
            vv[i]=w;
        }
        dfs(0,vv[0]);
        printf("%d\n",maxx);
    }
    return 0;
}

学长标程好像跟数塔类似,从下至上的找最大的。。。

下面是啸爷的标程。。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 9999991

using namespace std;

int head[10010];

struct E
{
    int v,next;
}edge[10010];

int Top;

int Link(int u,int v)
{
    edge[Top].v = v;
    edge[Top].next = head[u];
    head[u] = Top++;
}

int value[10010];

int dfs(int root)
{
    int temp = 0;

    for(int p = head[root];p != -1; p = edge[p].next)
    {
        temp = max(temp,dfs(edge[p].v));
    }

    value[root] += temp;

    return value[root];
}

int main()
{
    freopen("data1.in","r",stdin);
    freopen("data1.out","w",stdout);
    int n,i,j,v,w;

    while(scanf("%d",&n) != EOF)
    {
        memset(head,-1,sizeof(head));
        Top = 0;

        for(i = 1;i <= n; ++i)
        {
            scanf("%d %d",&v,&w);
            Link(v,i);
            value[i] = w;
        }

        printf("%d\n",dfs(1));
    }
    return 0;
}

SDUT2013级测试赛_D,布布扣,bubuko.com

时间: 2024-08-10 15:05:04

SDUT2013级测试赛_D的相关文章

2018冬令营模拟测试赛(三)

2018冬令营模拟测试赛(三) [Problem A]摧毁图状树 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见"试题描述" 输出示例 见"试题描述" 数据规模及约定 见"试题描述" 题解 这题没想到贪心 QwQ,那就没戏了-- 贪心就是每次选择一个最深的且没有被覆盖的点向上覆盖 \(k\) 层,因为这个"最深的没有被覆盖的点"不可能再有其它点引出的链覆盖它了,而它又

2018冬令营模拟测试赛(十九)

2018冬令营模拟测试赛(十九) [Problem A]小Y 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见"试题描述" 输出示例 见"试题描述" 数据规模及约定 见"试题描述" 题解 目前未知. 这题目前就能做到 \(O(n \sqrt{M} \log n)\),其中 \(M\) 是逆序对数,然而会被卡 \(T\):当然这题暴力可以拿到和左边那个算法一样的分数,只要暴力加一个剪枝:当左

【2016北京集训测试赛(八)】 crash的数列

Description 题解 题目说这是一个具有神奇特性的数列!这句话是非常有用的因为我们发现,如果套着这个数列的定义再从原数列引出一个新数列,它居然还是一样的...... 于是我们就想到了能不能用多点数列套着来加速转移呢? 但是发现好像太多数列套起来是可以烦死人的...... 我们就采用嵌套两次吧,记原数列为A,第一层嵌套为B,第二层嵌套为C. 我们其实可以发现一些规律,对于Ci,它对应了B中i的个数:对于Bi,它对应了A中i的个数. 稍加处理即可,我们一边计算一边模拟数列的运算,同时可以计算

计蒜之道 测试赛 (BCD)

测试赛写写题解不会被吐槽吧... 淘汰赛车 时限:1000ms 内存:262144K 赛车比赛在潘多拉星球变得越来越流行了.但是他们的比赛跟我们平常的不太一样:n 辆赛车在一条长长的直道上展开同台竞技.每辆赛车的速度都为 1m/s,整条赛道在每一米都有坐标标记. 在比赛的赛车中,赛车 i 从 0 秒开始由 ai 向 bi 移动.到达 bi 之后转而返回由 bi 向 ai 移动.循环往复. 又是蒜头菌!原来这是蒜头菌正在玩的一个手机小游戏.蒜头菌可以在某些位置放下 TNT 炸毁某些赛车.因为他有

在mysql数据库中制作千万级测试表

在mysql数据库中制作千万级测试表 前言: 最近准备深入的学一下mysql,包括各种引擎的特性.性能优化.分表分库等.为了方便测试性能.分表等工作,就需要先建立一张比较大的数据表.我这里准备先建一张千万记录用户表. 步骤: 1 创建数据表(MYISAM方式存储插入速度比innodb方式快很多)   数据表描述 数据量:1千万 字段类型: id :编号 uname:用户名 ucreatetime: 创建时间 age:年龄 CREATE TABLE usertb(    id serial,   

测试赛A - Colored Sticks(并查集+字典树+欧拉回路)

A - Colored Sticks Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sti

测试赛B - Balance(天平的dp问题)

B - Balance Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It

测试赛D - The War(有控制范围的贪心)

D - The War Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description A war had broken out because a sheep from your kingdom ate some grasses which belong to your neighboring kingdom. The counselor of your k

测试赛F - Dragon Balls(并查集)

F - Dragon Balls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to