A training academy keeps a roster of cadets. Every cadet has a unique ID, a name, an age, and, optionally, a single mentor who is also a cadet on the roster (identified by that mentor's ID); a cadet with no mentor is recorded with mentor ID 0. No cadet can indirectly mentor themselves — the mentor relationships never form a cycle.
For every cadet who is the direct mentor of at least one other cadet, report that mentor's ID, name, the number of cadets who directly report to them (not counting mentees of mentees), and the average age of those direct mentees, rounded to the nearest integer (round exact halves up, e.g. 21.5 rounds to 22). Print one line per qualifying mentor, ordered by ascending mentor ID. If no cadet mentors anyone, print nothing.
n — the number of cadets.n lines each contain: id name reportsTo age, where id is the cadet's unique positive integer ID, name is a single token with no whitespace, reportsTo is 0 or the ID of another cadet in the roster who is this cadet's direct mentor, and age is a positive integer.For each mentor ID (ascending) that has at least one direct mentee, print one line: id name count avgAge, space-separated, where count is the number of direct mentees and avgAge is their average age rounded to the nearest integer as described above. If there are no such mentors, print nothing.
1 <= n <= 10001 <= id <= 100000, all IDs distinct.reportsTo is 0 or the ID of a distinct cadet appearing somewhere in the roster; the mentor relationships never contain a cycle.18 <= age <= 100name consists of 1 to 20 alphanumeric characters and contains no spaces.Example 1
Input
5 1 Alice 0 30 2 Bob 1 25 3 Carol 1 35 4 Dan 2 40 5 Eve 2 20
Expected
1 Alice 2 30 2 Bob 2 30
Explanation
Alice (id 1) directly mentors Bob and Carol, whose ages are 25 and 35, averaging 30. Bob (id 2) directly mentors Dan and Eve, whose ages are 40 and 20, also averaging 30. Both have exactly 2 mentees. Sorted by mentor ID: `1 Alice 2 30` then `2 Bob 2 30`.
Example 2
Input
3 10 Xen 0 20 11 Yara 10 21 12 Zeke 10 22
Expected
10 Xen 2 22
Explanation
Xen (id 10) directly mentors Yara (age 21) and Zeke (age 22). Their average age is (21+22)/2 = 21.5, which rounds up to 22. Neither Yara nor Zeke mentors anyone, so the only output line is `10 Xen 2 22`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →