CF--831--E

关键

f(i)(0,1)代表选和不选
如果我要选,就必须从选的那个儿子转移过来,因为我本身这个数最后如果要贡献上,就一定只能贡献给最后去掉的那个儿子,并且儿子必须是要选的
如果我不选,就儿子的最大累加就可以了

代码

#include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using pdd=pair<double,double>;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define TT int _=read();while(_--)
#define int long long
using ll=long long;
const ll inf=1e18;
//#define double long double
#define endl '\n'
const int M=1e6+5;

inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}

inline void print(int x) {
    if(x<0){putchar('-');x=-x;}
    if(x/10)print(x/10);
    putchar(x%10+'0');
}
//数字随便选的
vector<int>v[M];
int f[M][2];
int ans;
void dfs(int now) {
    f[now][1]=1;
    if(v[now].size()==0)return ;
    int mx=0;
    for(auto to:v[now]) {
        dfs(to);
        f[now][0]+=max(f[to][0],f[to][1]);
        f[now][1]=max(f[now][1],f[to][1]+1);
    }
}

signed main() {
    int n=read();
    for(int i=2;i<=n;i++) {
        int x=read();
        v[x].push_back(i);
    }
    dfs(1);
    cout<<max(f[1][0],f[1][1]);
    return 0;
}
posted @ 2022-12-29 21:10  basicecho  阅读(27)  评论(0)    收藏  举报