## Algorithm

Problem Name: Data Structures - Company Retreat

In this HackerRank in Data Structures - Company Retreat solutions,

The LRT Company has n employees. Each employee has a unique ID number from 1 to n , where the director's ID is number 1. Every employee in the company has exactly one immediate supervisor — except the director, who has no supervisor. The company's employee hierarchy forms a tree of employee IDs that's rooted at employee number 1 (the director). The director decides to have a retreat lasting m days. Each day, the employees will be assigned to different groups for team building exercises. Groups are constructed in the following way:

• An employee can invite their immediate supervisor (the director has no supervisor and, thus, doesn't invite anyone). If employee is invited by employee b, then a and b are considered to be in the same group.
• Once an employee is invited to be in a group, they are in that group. This means that if two employees have the same immediate supervisor, only one of them can invite that supervisor to be in their group.
• Every employee must be in a group, even if they are the only employee in it.

The venue where LRT is hosting the retreat has different pricing for each of the m days of the retreat. For each day j there is a cost of dj dollars per group and a per-group size limit of pj

(i.e., the maximum number of people that can be in any group on that day).

Help the director find optimal groupings for each day so the cost of the

-day retreat is minimal, then print the total cost of the retreat. As this answer can be quite large, your answer must be modulo 109 + 7.

Input Format

The first line contains two space-separated integers denoting the respective values of n (the number of employees) and m (the retreat's duration in days).
The next line contains n - 1 space-separated integers where each integer i denotes si (1 <= i <= n) which is the ID number of employee i's direct supervisor.
Each line j of the m subsequent lines contain two space-separated integers describing the respective values of dj (the cost per group in dollars) and pj (the maximum number of people per group) for the jth day of the retreat.

Constraints

• 1 <= n,m <= 105
• 1 <= si <= n
• 1 <= dj, pj <= 109

• 1 <= n,m <= 2000 for 40% of the maximum possible score.

Output Format

Print a single integer denoting the minimum total cost for the m-day retreat. As this number can be quite large, print your answer modulo 109 + 7.

Sample Input

7 3
1 1 3 4 2 4
5 3
6 2
1 1


Sample Output

46


Explanation

In the Sample Case above, the company has 7 employees and the retreat goes on for 3 days. The hierarchy looks like this: On the first day, the cost per group is 5 dollars and each group has a maximum size of 3. The employees split into the following three groups:
1. Employee 6 invites their manager, employee 2. Employee 2 then invites their manager, employee 1 (the director).
2. Employee 5 invites their manager, employee 4 . Employee 4 then invites their manager, employee 3.
3. Employee 7's manager is already in another group, so they are in a group by themself.

These groupings are demonstrated in the following image where each group has a different pattern: In other words, the final groups are {1,2,6},{3,4,5} and {7} This means the total cost for the first day is groups * cost = 3 * 5 = 15 dollars. On the second day, they split into 4 groups with a maximum size of 2 at a total cost of 24 dollars. On the third day, they split into 7 groups of size 1 at a total cost of 7 dollars. When we sum the costs for all three days, we get 15 + 24 +7 = 46 % 109 + 7 = 46

## Code Examples

### #1 Code Example with C++ Programming

Code - C++ Programming


#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 1 << 17;
const int LOG = 17;

int n, m, tick, cnt;
int dep[N], st[N], nd[N], a[N], leaf[N];
vector < int > v[N], q[N];

int t[N << 1], sparse[LOG][N];

void up(int x, int k) {
t[x += N] = k;
while(x > 1) {
x >>= 1;
t[x] = min(t[x + x], t[x + x + 1]);
}
}

int get(int l, int r) {
int res = 1e9;
for(l += N, r += N; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) {
if(l & 1) res = min(res, t[l]);
if(~r & 1) res = min(res, t[r]);
}
return res;
}

void dfs(int p, int x) {
st[x] = ++tick;
dep[x] = dep[p] + 1;
sparse[x] = p;
for(int i = 1; i < LOG; i++)
sparse[i][x] = sparse[i - 1][sparse[i - 1][x]];
leaf[x] = 1e9;
for(auto u : v[x]) {
dfs(x, u);
leaf[x] = min(leaf[x], leaf[u] + 1);
}
if(leaf[x] > 5e8) {
leaf[x] = 0;
cnt++;
}
q[leaf[x]].push_back(x);
nd[x] = tick;
}

int calc(int group) {
int res = cnt;
priority_queue < ii > Q;
for(auto x : q[group])
Q.push({dep[x], x});
vector < int > vv;
while(!Q.empty()) {
int x = Q.top().second;
Q.pop();
if(leaf[x] < group or get(st[x], nd[x]) - dep[x] < group)
continue;
vv.push_back(x);
up(st[x], dep[x]);
res++;
if(dep[x] > group) {
int k = group;
for(int i = LOG - 1; i >= 0; i--) {
if(k >= (1 << i)) {
k -= 1 << i;
x = sparse[i][x];
}
}
Q.push({dep[x], x});
}
}
for(auto x : vv)
up(st[x], 1e9);
return res;
}

int main () {

for(int i = 1; i < N + N; i++)
t[i] = 1e9;

scanf("%d %d", &n, &m);

for(int i = 2; i <= n; i++) {
int x;
scanf("%d", &x);
v[x].push_back(i);
}

dfs(0, 1);

for(int i = 1; i <= n; i++)
a[i] = calc(i);

ll ans = 0;

for(int i = 1; i <= m; i++) {
int x, y;
scanf("%d %d", &x, &y);
ans += (ll) x * a[min(n, y)];
ans %= (int) 1e9 + 7;
}

printf("%lld\n", ans);

return 0;

}

Copy The Code &

### #2 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.util.*;

public class Solution {

static final int logN = 17;
static final int INF = 1000000000;
static final int MOD = 1_000_000_007;

static int[] nxtV;
static int[] succV;
static int[] ptrV;
static int indexV = 1;

static void addV(int u, int v) {
nxtV[indexV] = ptrV[u];
ptrV[u] = indexV;
succV[indexV++] = v;
}

static int[] nxtG;
static int[] succG;
static int[] ptrG;
static int indexG = 1;

static void addG(int u, int v) {
nxtG[indexG] = ptrG[u];
ptrG[u] = indexG;
succG[indexG++] = v;
}

static int tick = 0;
static int[][] lca;
static int[] start;
static int[] dist;
static int[] depth;
static int[] d;
static int[] finish;

static class NodeDfs {
int u;
int p;
boolean start = true;
boolean flag = true;

public NodeDfs(int u, int p) {
this.u = u;
this.p = p;
}
}

static void dfs() {
while (!deque.isEmpty()) {
NodeDfs node = deque.peekLast();
if (node.start) {
lca[node.u] = node.p;
start[node.u] = ++tick;
dist[node.u] = INF;
depth[node.u] = depth[node.p] + 1;
d[start[node.u]] = depth[node.u];
for (int i = ptrV[node.u]; i > 0; i = nxtV[i]) {
int v = succV[i];
if (v != node.p) {
node.flag = false;
}
}

node.start = false;
} else {
if (node.flag) {
dist[node.u] = 0;
}
finish[node.u] = tick;

dist[node.p] = Math.min(dist[node.p], dist[node.u] + 1);

deque.removeLast();
}
}
}

static int up(int node, int k) {
for (int i = logN; i >= 0; i--) {
if ((k & (1 << i)) > 0) {
node = lca[i][node];
}
}
return node;
}

static int[] segTree;

static int update(int k, int bas, int son, int x, int y) {
if (bas > x || son < x) {
return segTree[k];
}
if (bas == son) {
return segTree[k] = (y == 1 ? d[bas] : INF);
}
return segTree[k] =
Math.min(
update(k + k, bas, (bas + son) >> 1, x, y),
update(k + k + 1, ((bas + son) >> 1) + 1, son, x, y));
}

static int query(int k, int bas, int son, int x, int y) {
if (bas > y || son < x) {
return INF;
}
if (x <= bas && son <= y) {
return segTree[k];
}
return Math.min(
query(k + k, bas, (bas + son) >> 1, x, y),
query(k + k + 1, ((bas + son) >> 1) + 1, son, x, y));
}

static class Pair implements Comparable {

private int first;
private int second;

public Pair(int first, int second) {
this.first = first;
this.second = second;
}

public int compareTo(Pair other) {
if (first < other.first) return 1;
else if (first > other.first) return -1;
else if (second < other.second) return 1;
else if (second > other.second) return -1;
else return 0;
}
}

public static void main(String[] args)
throws IOException {
BufferedWriter bw = new BufferedWriter(
new FileWriter(System.getenv("OUTPUT_PATH")));

int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());

nxtV = new int[n + 1];
succV = new int[n + 1];
ptrV = new int[n + 1];

for (int i = 2; i <= n; i++) {
int item = Integer.parseInt(st.nextToken());
}

nxtG = new int[n + 1];
succG = new int[n + 1];
ptrG = new int[n + 1];

lca = new int[logN + 1][n + 1];
start = new int[n + 1];
dist = new int[n + 1];
depth = new int[n + 1];
d = new int[n + 1];
finish = new int[n + 1];

dfs();

for (int i = 1; i <= logN; i++) {
for (int j = 1; j <= n; j++) {
lca[i][j] = lca[i - 1][lca[i - 1][j]];
}
}

int cur = 0;
segTree = new int[(n + 1) << 2];

for (int i = 1; i <= n; i++) {
if (dist[i] == 0) {
update(1, 1, n, start[i], 1);
cur++;
} else {
update(1, 1, n, start[i], 0);
}
}

List del = new ArrayList<>();
PriorityQueue queue = new PriorityQueue<>();
int[] ans = new int[n + 1];

ans = n;

for (int i = 2; i <= n; i++) {
int all = cur;
for (int j = ptrG[i]; j > 0; j = nxtG[j]) {
int v = succG[j];
}
del.clear();
while (!queue.isEmpty()) {
int node = queue.remove().second;
if (query(1, 1, n, start[node],
finish[node]) - depth[node] < i) {
continue;
}
update(1, 1, n, start[node], 1);
all++;
int up = up(node, i);
if (up > 0) {
}
}
ans[i] = all;
for (int j = 0; j < del.size(); j++) {
update(1, 1, n, start[del.get(j)], 0);
}
}

long result = 0;
for (int i = 1; i <= m; i++) {
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
result = (result + (
ans[Math.min(n, y)] * (long) x) % MOD) % MOD;
}

bw.write(String.valueOf(result));
bw.newLine();

bw.close();
br.close();
}
}

Copy The Code &