老坛脚踩酸菜


私信TA

用户名:3309028585

访问量:453

签 名:

等  级
排  名 59853
经  验 192
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:使用邻接表存储树。以当前节点为根的树转换后的最大高度是子节点数量加上子树的最大高度

注意事项:

参考代码:

#include<iostream>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=1e5+10;

int h[N],e[N],ne[N],idx;

int f[N];

void add(int a,int b)

{

    e[idx]=b,ne[idx]=h[a],h[a]=idx++;

}

int dp(int a)

{

    int res=0,sum=0;

    for(int i=h[a];i!=-1;i=ne[i])

    {

        int j=e[i];

        res++;

        sum=max(sum,dp(j));

    }

    res+=sum;

    return res;

}

int main()

{

    int n;

    cin>>n;

    memset(h,-1,sizeof h);

    for(int i=2;i<=n;i++)

    {

        int a;

        cin>>a;

        add(a,i);

    }

    int res=dp(1);

    cout<<res;

    return 0;

}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区