天天看點

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;
}
           
bfs