Subordinates#11429

Subordinates (Editorial)

The structure of the company is a tree. For each vertex in the tree, determine the number of vertices in the subtree where is the root. The number of subordinates for employee will be sum[v] - 1 (since employee is not his own subordinate).

Initiate a depth-first search starting from vertex , which corresponds to the general director of the company. If to_1, to_2, ..., to_k are the children of vertex , then

sum[v] = 1 + sum[to_1] + sum[to_2] + ... + sum[to_k]

If vertex is a leaf in the tree, set sum[v] = 1.

Example

Consider the example. Next to each employee (vertex of the graph), the number of their subordinates is indicated.

Algorithm implementation

The tree is stored in the adjacency list .

vector<vector<int> > g;

The dfs function performs a depth-first search from vertex and returns the number of vertices in the subtree rooted at . Vertex is the parent of in the depth-first search.

int dfs(int v, int p)
{

Initially, the subtree rooted at contains only the vertex .

  sum[v] = 1;

Consider the edge v → to. If to \ne p, add to the number of vertices in the subtree rooted at .

  for (int to : g[v])
    if (to != p) sum[v] += dfs(to, v);

Return the number of vertices in the subtree rooted at .

  return sum[v];
}

The main part of the program. Read the input data. Construct a tree.

scanf("%d", &n);
g.resize(n + 1);

for (i = 2; i <= n; i++)
{
  scanf("%d", &x);

For employee , its immediate supervisor in the company is .

  g[i].push_back(x);
  g[x].push_back(i);
}

Start the depth-first search from vertex .

sum.resize(n + 1);
dfs(1, -1);

Print the answer. The number of subordinates of employee is equal to .

for (i = 1; i <= n; i++)
  printf("%d ", sum[i] - 1);
printf("\n");

Java implementation


import java.util.*;

public class Main
{
  static ArrayList<Integer>[] g;	
  static int sum[];
  static int n;
  
  static int dfs(int v, int p)
  {
    sum[v] = 1;
    for(int to : g[v])
      if (to != p) sum[v] += dfs(to,v);
    return sum[v];
  }
  
  @SuppressWarnings("unchecked")
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    n = con.nextInt();
    g = new ArrayList[n+1]; // unchecked
    sum = new int[n+1];

    for (int i = 0; i <= n; i++)
      g[i] = new ArrayList<Integer>();

    for (int i = 2; i <= n; i++)
    {
      int x = con.nextInt();
      g[i].add(x);
      g[x].add(i);
    }
    
    dfs(1,-1);
    for (int i = 1; i <= n; i++)
      System.out.print(sum[i] - 1 + " ");    
    System.out.println();
    con.close();
  }
}   

Python implementation

Increase the stack for recursion.

import sys
sys.setrecursionlimit(1000000)

The dfs function performs a depth-first search from vertex and returns the number of vertices in the subtree rooted at . Vertex is the parent of in the depth-first search.

def dfs(v, p):

Initially, the subtree rooted at contains only the vertex .

  sum[v] = 1

Consider the edge v → to. If to \ne p, add to the number of vertices in the subtree rooted at .

  for to in g[v]:
    if to != p: sum[v] += dfs(to,v)

Return the number of vertices in the subtree rooted at .

  return sum[v]

The main part of the program. Read the number of employees.

n = int(input())

Initialize the adjacency list of the graph .

g = [[] for _ in range(n+1)]

Read the input data. Construct a tree.

lst = list(map(int, input().split()))
for i in range(2,n+1):
  a = lst[i-2]

For employee i~(i \ge 2), its immediate supervisor in the company is .

  g[a].append(i)
  g[i].append(a)

Initialize the list .

sum = [0] * (n + 1)

Start the depth-first search from vertex .

dfs(1,-1)

Print the answer. The number of subordinates of employee is equal to sum[i] - 1.

for i in range(1, n+1):
  print(sum[i] - 1,end=" ")