Codeforces-115a-party

Source: Internet
Author: User

115a-party

A company had n employees numbered from 1 to N. Each employee either have no immediate manager or exactly one immediate manager, who was another employee with a different n Umber. An employee A was said to being the superior of another employee B if at least one of the following is true:

Employee A is the immediate manager of employee B
Employee B has an immediate manager employee C such this employee A is the superior of employee C.
The company won't have a managerial cycle. That is, there would not exist a employee who is the superior of his/her own immediate manager.

Today The company was going to arrange a party. This involves dividing all n employees into several Groups:every employee must belong to exactly one group. Furthermore, within any single group, there must isn't be--employees A and b such that's A is the superior of B.

What is the minimum number of groups this must be formed?

Input
The first line contains integer n (1?≤?n?≤?2000)-the number of employees.

The next n lines contain the integers pi (1?≤?pi?≤?n or pi?=?-1). Every pi denotes the immediate manager for the I-th employee. If Pi Is-1, that means that the i-th employee does not has an immediate manager.

It is guaranteed, and that no employee would be the immediate manager of Him/herself (Pi?≠?i). Also, there'll be no managerial cycles.

Output
Print a single integer denoting the minimum number of groups, that'll be formed in the party.

input
5
-1
1
2
1
-1
Output
3

Title Link: cf-115a

Everyone has 0 or 1 direct bosses. Open the party group, ask at least a few groups, so that each group inside members have no boss relationship (direct, indirect).

The idea of the topic: Just beginning to think is and check set, later found not, in fact, is a few trees, the highest number of trees. To simplify, enter the parent node number for each member's boss. Searching from the leaf node to find the maximum number of layers is the answer.

Here's the code:

////115a-party.cpp//Codeforce////Created by Pro on 16/5/18.//Copyright (c) 2016 Loy. All rights reserved.//#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>#include <iomanip>using namespace STD;intfa[2005];intMain () {intNCin>> N; for(inti =1; I <= N; i++)Cin>> Fa[i];intans=0; for(inti =1; I <= N; i++) {intTMP =0; for(intj = i; J <= N && J! =-1;        j = Fa[j]) {tmp++;    } ans = max (ans,tmp); }cout<< ans << Endl;return 0;}

Codeforces-115a-party

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.