Codeforces 115A. Party

仔细分析题意后,发现直接求所有树的深度即可,最大的深度即为答案。

#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

struct node{
    int x,step;
};
vector<int>G[2222];
vector<int>G2;
int bfs(int x){
    vis[x] = 1;
    queue<node>Q;
    node s ;s.x = x;s.step = 1;
    Q.push(s);
    int maxx = 1;
    while(!Q.empty()){
        node now = Q.front();Q.pop();
        //cout<<now.x<<endl;
      //  system("pause");
        node nt;
        maxx = max(maxx,now.step);
        for(int i=0;i<G[now.x].size();i++){
            nt.x = G[now.x][i];
            nt.step = now.step+1;
            Q.push(nt);
           // cout<<nt.x<<endl;
        }
    }
    return maxx;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int tmp;scanf("%d",&tmp);
        if(tmp!=-1){
            G[tmp].push_back(i);
        }
        else{
            G2.push_back(i);
        }
    }
    int maxx = 1;
   // cout<<G2.size();
    for(int i=0;i<G2.size();i++){
       maxx = max(maxx,bfs(G2[i])) ;
    }
    printf("%d\n",maxx);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值