
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
We are given a tree which can have endless levels, a variable child which will store the number of children a node can have, a variable weight which will store the weight associated with the path and a variable path that will store the path and task is to calculate the count of number of paths which has weights equals to the X and there must be an at least one edge with the given weight.
For Example
Input - int child = 4, weight = 4, path = 4;
Output - Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: 1
Explanation - As we are given with the node having 4 children connected with 4 paths and have weight of 4 associated with the path. So, we can see that there can only be one path with the weight as 4 i.e. 1 - 4 therefore the count is 1.
Input - int child = 3, weight = 2, path = 4;
Output - Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: 4
Explanation - As we are given with the node having 3 children connected with 4 paths and have weight of 2 associated with the path. So, we can see that there can be four paths with the weight as 2 i.e. 1-1, 1 - 2, 2-1 and 2-1 therefore, the count is 4.
Approach used in the below program is as follows
- Input the total number of children, paths and weight associated with each path in the variable's child, weight and path respectively.
- Declare an array of given size.
- Start loop FOR from i to 0 till the size of an array. Inside the loop, start another loop FOR from j to 0 till j less than 2 and then set arr[i][j] as -1.
- Now call the function total_weight() by passing path, 0, weight, child and arr as an argument to the function.
- Inside the function,
- Declare a temporary variable count to store the result.
- Check IF path less than 0 then return 0
- Check IF path equals 0 then return i
- Check IF arr[path][i] not equals to 1 then return arr[path][i]
- Start loop FOR from j to 1 till child. Inside the loop, check IF j equals weight than set count as a recursive call to the function total_weight() function by passing path-j, 1, weight, child and arr to the function as an argument.
- Else, set count as a recursive call to the function total_weight() function by passing path-j, i, weight, child and arr to the function as an argument.
- Set arr[path][i] as count and
- Return arr[path][i]
- print the result.
Example
#include <bits/stdc++.h> using namespace std; #define size 4 #define col 4 int total_weight(int path, int i, int weight, int child, int arr[size + 1][col]) { int count = 0; if (path < 0) { return 0; } if (path == 0) { return i; } if (arr[path][i] != -1) { return arr[path][i]; } for (int j = 1; j <= child; j++) { if (j == weight) { count += total_weight(path - j, 1, weight, child, arr); } else { count += total_weight(path - j, i, weight, child, arr); } } arr[path][i] = count; return arr[path][i]; } int main() { int child = 4, weight = 4, path = 4; int arr[size + 1][col]; for (int i = 0; i <= size; i++) { for (int j = 0; j < 2; j++) { arr[i][j] = -1; } } cout << "Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: " << total_weight(path, 0, weight, child, arr); }
If we run the above code it will generate the following output −
Output
Count of number of paths whose weight is exactly X and has at-least one edge of weight M are: 1
- Related Articles
- Count the nodes whose weight is a perfect square in C++
- Program to count number of paths whose sum is k in python
- Find the Number of Paths of Weight W in a K-ary tree using C++
- C++ code to count number of weight splits of a number n
- Count the nodes in the given tree whose sum of digits of weight is odd in C++
- Count the nodes in the given tree whose weight is a power of two in C++
- How to check if a string has at least one letter and one number in Python?
- Get at least x number of rows in MySQL?
- What Exactly Is Whey Protein? The Health, Weight, and Side Effects to Know
- Make the sample space of tossing two coins simultaneously and find the probability of:1. Exactly two heads2. At least one head3. At least one tail4. At most two tails
- C++ Program to Find Maximum Number of Edge Disjoint Paths
- What is the weight of a body on earth whose mass is 25 kg?
- The weight of 72 books is 9 kg. What is the weight of 40 such books?
- Count of sub-strings that contain character X at least once in C++
- What is the weight of kidney ?
