HackTree

 You are given a tree with n nodes. Each node has a value assigned with it. The cost of a path is defined as the summation of all the values assigned to nodes that belong to the path.

The root of the tree is node number 1.

Cost of path example

Unirected graph with nodes numbered 1-7, edges indicating child nodes, and each node assigned a distinct value from 10 to 42.

The cost of the path 6 -> 5 -> 3 -> 1 in the above tree is 42 + 31 + 20 + 10 = 103.

A Vertical Path in a tree is the path that is going up towards the root of the tree. It is not necessary for the path to end at the root.

Given a tree with n nodes and an integer k. Find the number of vertical paths such that the (cost of the path) % k = 0 where % represents the modulo operation.

Note: The modulo operation returns the remainder of a division after one number is divided by another. For example - 5 % 2 = 1.

Example

k = 2

tree =

Undirected graph with 4 nodes. Node 1 connects to 2 and 4, node 4 connects to 3. All have a value of 1.

There are a total of 8 vertical paths:

  1. 1
  2. 2
  3. 4
  4. 2->1
  5. 4->1
  6. 3
  7. 3->4
  8. 3->4->1

But only (2 -> 1), (4 -> 1), (3 -> 4) have (cost of the path) % k = 0.

Hence the answer is 3.

Function Description

Complete the function countVerticalPaths in the editor below.

countVerticalPaths has the following parameters:

    cost: the array representing the value of each node.

    edge_nodes: number of nodes in the tree.

    edge_from: integer array where the ith integer denotes one endpoint of the ith edge.

    edge_to: integer array where the ith integer denotes the other endpoint of the ith edge

    k: an integer

Returns

    int: the number of vertical paths with (cost of the path) % k = 0

    Note: The tests are generated in such a way that the returned value fits in int32

Constraints

  • 1 ≤ n ≤ 2*105
  • 0 ≤ cost[i]  ≤ 108
  • 1 ≤ k ≤ 105

Input Format For Custom Testing

The first line contains one integer, n denoting the size of the cost array.

The next n lines contain elements of the cost array.

The next line contains two integers, n and m (where m = n - 1), denoting the number of nodes and number of roads respectively.

Each line i of the m subsequent lines (where 0 ≤ i < m) contains two integers, u and v, denoting that nodes u and v are connected via an edge.

The next line contains an integer denoting k.Sample Case 0

Sample Input For Custom Testing

STDIN         FUNCTION
-----         --------
5         ->  n = Size of Cost array.
1 
2
2
1
2         ->  cost = {1,2,2,1,2}
5 4       ->  n = 5, m = 4
2 3
2 1
1 4
2 5       
2         ->  k = 2

Sample Output

6

Explanation

There are 6 vertical paths following the given condition:

Directed graph with nodes 1 to 5, edges from 1 to nodes 2, and 4, labeled 1, 2, 1. Node 2 connects to node 3 label 3 and 5 label 2.

these paths are:

1. 2

2. 5 -> 2

3. 4 -> 1

4. 3

5. 3 -> 2

6. 5

Hence the answer is 6.Sample Case 1

Sample Input For Custom Testing

STDIN         FUNCTION 
-----         -------- 
5         ->  n = 5
2 
3
0
3
0         ->  cost = {2,3,0,3,0}
5 4       ->  n = 5, m = 4 
2 3         
3 1
3 4
3 5
3         ->  k = 3

Sample Output

7

Explanation

There are 7 vertical paths following the given condition :

Undirected graph with nodes 1-5. Node 3 (value 0) connects to 1, 2, 4, and 5 with values 2, 3, 3, and 0 respectively.

these paths are :

1. 4

2. 4 -> 3

3. 2

4. 2 -> 3

5. 5

6. 3

7. 5 -> 3

Hence the answer is 7.Language

#include·<bits/stdc++.h>

/*

·*·Complete·the·'countVerticalPaths'·function·below.

·*

·*·The·function·is·expected·to·return·an·INTEGER.

·*·The·function·accepts·following·parameters:

·*··1.·INTEGER_ARRAY·cost

·*··2.·UNWEIGHTED_INTEGER_GRAPH·edge

·*··3.·INTEGER·k

·*/

/*

·*·For·the·unweighted·graph,·<name>:

·*

·*·1.·The·number·of·nodes·is·<name>_nodes.

·*·2.·The·number·of·edges·is·<name>_edges.

·*·3.·An·edge·exists·between·<name>_from[i]·and·<name>_to[i].

·*

·*/

int·countVerticalPaths(vector<int>·cost,·int·edge_nodes,·vector<int>·edge_from,·vector<int>·edge_to,·

int·k)·{

}

int main()

Comments

Popular posts from this blog

Cybersecurity Essentials 1.1 Final Quiz Answers Form B 100% 2022

Samsung Galaxy A13 5G With MediaTek Dimensity 700 SoC, 90Hz Display Launched: Price, Specifications

"Emphasis Laid On Local Languages For Conducting Entrance Exams"; President Kovind Hails New Education Policy

NPTEL DATA MINING WEEK 2 ANSWERS