INOI 2016: Wealth Disparity

In a company, there are N N employees labelled 1 , 2 , 3 , N 1,2,3 \ldots , N . The organizational structure of the company forms a tree-like hierarchy.

  • P i P_i denotes the manager of employee i i .
  • For each employee i i , A i A_i denotes the wealth of employee i i .

We say that employee j j is the subordinate of employee, i i if employee j j appears in a subtree rooted at i i .

Your task is to find a pair of employees ( i , j ) (i,j) , j j a subordinate of i i , such that A i A j A_i - A_j is maximized. Enter your answer as the value of A i A j A_i - A_j .

Here is the data.


Input Format

  • The first line contains N N .
  • The second line contains A 1 , A 2 , A N A_1, A_2, \ldots A_N .
  • The third line contains P 1 , P 2 , P N P_1, P_2, \ldots P_N . If i i is the boss of the company, he has no manager, i.e, P i = 1 P_i = -1

Example

  • Input:
1
2
3
4
5 10 6 12
2 -1 4 2

  • Expected Output: 6

  • Explanation : In this case, the possible choices to consider are (2,1) with a difference in wealth of of 5, (2,3) with 4, (2,4) with -2 and (4,3) with 6. So the answer is 6.


The answer is 199124525.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

The simplest way to solve this problem is to consider all pairs ( i , j ) (i,j) where j j is a subordinate of i i . To do this, we could pick each node and climb upto the root whilst keeping track of the maximum difference.

Clearly, this is a O ( N h ) O(N \cdot h) solution where h h stands for height. For a random tree, this tends to be O ( N log N ) O(N \log N) , however in the worst case, i.e, when the tree is a long chain, the complexity becomes O ( N 2 ) O (N^2) .


Turns out we can do better.

Notice that the maximum difference between a fixed node i i and some node j j in its subtree can be found by simply finding the minimum value in the subtree, which can be done using a DFS.

So, the key is to use a DFS to get the minimum at each subtree and for each node, check if the maximum possible difference from this node to the minimum in the corresponding subtree is greater than what we've encountered so far.

This is an O ( N ) O(N) solution since we run the processing at each node only once.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>

typedef std::vector<int> vi;
typedef std::vector<vi> vvi;

vi wealth;
vvi tree;

int maxDiff = -1000000;

int getMin(int node){
  int minHere = wealth[node];
  for (int u: tree[node]){
    int minBranch = getMin(u);
    minHere = std::min(minBranch, minHere);
    maxDiff = std::max(maxDiff, wealth[node]-minBranch);
  }
  return minHere;
}

int main(){
  int N;
  std::cin >> N;
  wealth.resize(N+1);
  for (int iii=1; iii <= N; iii++)
    std::cin >> wealth[iii];

  //convert to Adjacency List representation
  int root;
  tree.resize(N+1);
  for (int iii=1; iii <= N; iii++){
    int parent;
    std::cin >> parent;
    if (parent == -1)
      root = iii;
    else
      tree[parent].push_back(iii);
  }

  getMin(root);

  std::cout << maxDiff;

  return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...