Company overHead
Time: 3 s
Memory: 125 MB
Memory: 125 MB
There are \(N\) employees in a company, each with a unique ID from \(1\) to \(N\). Everyone works under one direct supervisor, except for one unfortunate soul who’s the Boss (\(ID = X\)). That person reports to no one because they’re too busy taking credit for everyone else’s work.
Now, each employee has a chaos rating, which is a measure of how much nonsense they bring to the office. This could be through excessive coffee consumption, passive-aggressive emails, or starting meetings that could’ve been emails. The chaos rating for employee \(i\) is \(c_i \).
Bob, who has nothing better to do, wants to know the most chaotic person in each employee’s chain of command, starting from themselves, going all the way up to the Boss. If multiple people in the chain have the same maximum chaos, the one with the smallest ID gets crowned the king/queen of chaos.
Now, each employee has a chaos rating, which is a measure of how much nonsense they bring to the office. This could be through excessive coffee consumption, passive-aggressive emails, or starting meetings that could’ve been emails. The chaos rating for employee \(i\) is \(c_i \).
Bob, who has nothing better to do, wants to know the most chaotic person in each employee’s chain of command, starting from themselves, going all the way up to the Boss. If multiple people in the chain have the same maximum chaos, the one with the smallest ID gets crowned the king/queen of chaos.
Input
The first line contains \(T\) the number of testcases.A line containing two integers \(N\) and \(X\): \(N\) is the number of employees, and \(X\) is the ID of boss
The second line contains \(N\) integers \(p_1, p_2, ... , p_N\) : The chaos values of the \(i\)th employee
The third line contains \(N\) integers \(s_1, s_2, ... , s_N\): Here \(s_i\) is the direct supervisor of employee \(i\).
And the value of \(s_x\) is \(-1\) (for the boss)
Constraint
1 <= T <= 100001 <= N <= 200000
1 <= X <= N
1 <= p[i] <= 10^9
1 <= s[i] <= N for all i != x and s[x] = -1
It is guaranteed that the sum of N over all testcases does not exceed 200000
Output
Print \(N\) lines. Each line should contain two integers for employee \(i\):The maximum chaos rating among all people in their management chain (including themselves)
The smallest ID among those with the maximum chaos rating
Examples
| Input | Output |
|---|---|
|
1
7 1 3 2 10 6 1 6 4 -1 1 1 2 2 3 3 |
3 1
3 1 10 3 6 4 3 1 10 3 10 3 |
Notes
Explanation of the sample testcase:Employee \(1\) is the Boss (since \(x = 1\) ). For each employee \(i\), we look at the chain \(i\) → \(...\) → \(1\) and pick the maximum chaos rating, breaking ties by smallest ID:
| Employee \( i\) | Chain | Chaos ratings along chain | Max chaos | Winner ID | Output |
|---|---|---|---|---|---|
| \(1\) | \(1\) | \([3] \) | \(3\) | \(1\) | 3 1 |
| \(2\) | \(2\) → \(1 \) | \([2, 3]\) | \(3\) | \(1\) | 3 1 |
| \(3\) | \(3\) → \(1\) | \([10, 3]\) | \(10\) | \(3\) | 10 3 |
| \(4\) | \(4\) → \(2\) → \( 1 \) | \([6, 2, 3]\) | \(6\) | \(4\) | 6 4 |
| \(5\) | \(5\) → \(2\) → \(1\) | \([1, 2, 3]\) | \(3\) | \(1\) | 3 1 |
| \(6\) | \(6\) → \(3\) → \(1\) | \([6, 10, 3]\) | \(10\) | \(3\) | 10 3 |
| \(7\) | \(7\) → \(3\) → \(1\) | \([4, 10, 3]\) | \(10\) | \(3\) | 10 3 |
Thus we get the sample output.
Problem Info
| Problem ID | 510 |
| Time Limit | 3000 ms |
| Memory Limit | 128000 KB |
| Moderators | Dr_KeK , refred1 , tafsiruzzaman , fahimcp495 , osama_bq , meheraj_hossain_ , sagorahmedmunna , Rakib , amirhozaifa , puja_chakraborty , shamima_ananna |
Statistics
Submit
You need to Login or Registration for submit your solution