Skip to content

Instantly share code, notes, and snippets.

@bertolo1988
Last active June 5, 2016 00:34
Show Gist options
  • Save bertolo1988/95b4a9c0138cb093a5db785d27c36b43 to your computer and use it in GitHub Desktop.
Save bertolo1988/95b4a9c0138cb093a5db785d27c36b43 to your computer and use it in GitHub Desktop.
/**
* @author bertolo1988
* http://codeforces.com/problemset/problem/115/A
* 115A-Party
*/
import java.util.*;
public class Party {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
LinkedHashMap<String, String> bossesMap = new LinkedHashMap<String, String>();
int n = keyboard.nextInt();
for (int i = 1; i <= n; i++) {
String parentLabel = keyboard.next();
if (!parentLabel.equals("-1") && parentLabel.length() > 0) {
bossesMap.put(Integer.toString(i), parentLabel);
} else {
bossesMap.put(Integer.toString(i), null);
}
}
System.out.println(retrieveMinimumPartyGroups(bossesMap));
keyboard.close();
}
private static int retrieveMinimumPartyGroups(LinkedHashMap<String, String> bossesMap) {
TreeSet<String> depths = new TreeSet<String>();
bossesMap.keySet().forEach(key -> depths.add(calcDepth(key, bossesMap)));
return depths.size();
}
private static String calcDepth(String key, LinkedHashMap<String, String> bossesMap) {
int depth = 1;
for (String cursor = bossesMap.get(key); cursor != null; cursor = bossesMap.get(cursor)) {
depth++;
}
return Integer.toString(depth);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment