题解
1.多根树结构,但是将-1的点设为0的子节点,就变成了单根树
2.仔细读题!!!只要同一链上的就不能在一个组里
code
#include<bits/stdc++.h>
using namespace std;
int depth[2005]={0};
vector<int> G[2005];
void dfs(int now)
{
for(auto next:G[now])
{
depth[next]=depth[now]+1;
dfs(next);
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(x==-1) x++;
G[x].push_back(i);
}
dfs(0);
int ans=0;
for(int i=1;i<=n;i++) ans=max(ans,depth[i]);
cout<<ans;
return 0;
}
标签:int,dfs,next,depth,Party,now
From: https://www.cnblogs.com/pure4knowledge/p/18194724