116C - Party - CodeForces Solution


dfs and similar graphs trees *900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define loop(i,a,n) for(int i = a; i <= n; i++)
#define loopr(i,n,a) for(int i = n; i >= a; i--)
#define foor(i,n) for(int i = 0; i < n; i++)
#define fore(el,v) for(auto& el:v)
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sz(v) (int)v.size()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define fast ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
#define endl '\n'

using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
using vpi = vector<pair<int,int>>;
using vvi = vector<vector<int>>;

const int MOD = 1e9+7;
const int OO = 1e9+5;
const int N = 2e5+5;

vi adj[N];
int ans, par[N];

void dfs(int v, int d){
    ans = max(ans, d);
    for(auto u : adj[v]){
        dfs(u, d+1);
    }
}

void TC()
{
    int n, p; cin >> n;
    loop(i,1,n){
        cin >> p;
        par[i] = p;
        if(p>0) adj[p].pb(i);
    }
    loop(i,1,n){
        if(par[i] != -1)
            continue;
        dfs(i,1);
    }
    cout << ans;
}

int32_t main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#endif
    fast
    int t = 1;
//    cin >> t;
    while (t--)
    {
        TC();
        cout << '\n';
    }
    return 0;
}

  	 	 			   		    	 			 		   	


Comments

Submit
0 Comments
More Questions

233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football