Number of unique paths in tree such that every path has a value greater than K

Given a tree as set of edges such that every node has unique value. We are also given a value k, the task is to count the unique paths in the tree such that every path has a value greater than K. A path value is said to be > K if every edge contributing in the path is connecting two nodes both of which have values > K.

Examples:

Input:

Output: 9



Approach: The idea is to not form the tree with all the given edges. We only add an edge if it satisfies the condition of > k. In this case, a number of trees will be formed. While forming the different trees, we will only add the edge into the tree if both the node value are greater than K. After this, various number of trees will be created. Run a DFS for every node which in the end traverses the complete tree with which the node is attached and count the number of nodes in every tree. The number of unique paths for every tree which has X number of nodes is X * (X – 1) / 2.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the number of nodes
// in the tree using DFS
int dfs(int node, int parent, list<int>* adj, bool vis[])
{
  
    // Base case
    int ans = 1;
  
    // Mark as visited
    vis[node] = true;
  
    // Traverse for all children
    for (auto it : adj[node]) {
  
        // If not equal to parent
        if (it != parent)
            ans += dfs(it, node, adj, vis);
    }
    return ans;
}
  
// Function that returns the count of
// unique paths
int countPaths(list<int>* adj, int k, int maxn)
{
  
    // An array that marks if the node
    // is visited or not
    bool vis[maxn + 1];
    int ans = 0;
  
    // Initially marked as false
    memset(vis, false, sizeof vis);
  
    // Traverse till max value of node
    for (int i = 1; i <= maxn; i++) {
  
        // If not visited
        if (!vis[i]) {
  
            // Get the number of nodes in that 
            // tree
            int numNodes = dfs(i, 0, adj, vis);
  
            // Total unique paths in the current
            // tree where numNodes is the total
            // nodes in the tree
            ans += numNodes * (numNodes - 1) / 2;
        }
    }
    return ans;
}
  
// Function to add edges to tree if value
// is less than K
void addEdge(list<int>* adj, int u, int v, int k)
{
    if (u > k && v > k) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
}
  
// Driver code
int main()
{
    int maxn = 12;
  
    list<int>* adj = new list<int>[maxn + 1];
    int k = 3;
  
    // Create undirected edges
    addEdge(adj, 2, 11, k);
    addEdge(adj, 2, 6, k);
    addEdge(adj, 5, 11, k);
    addEdge(adj, 5, 10, k);
    addEdge(adj, 5, 12, k);
    addEdge(adj, 6, 7, k);
    addEdge(adj, 6, 8, k);
  
    cout << countPaths(adj, k, maxn);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to count the number of 
# nodes in the tree using DFS 
def dfs(node, parent, adj, vis): 
  
    # Base case 
    ans = 1
  
    # Mark as visited 
    vis[node] = True
  
    # Traverse for all children 
    for it in adj[node]: 
  
        # If not equal to parent 
        if it != parent: 
            ans += dfs(it, node, adj, vis) 
      
    return ans 
  
# Function that returns the 
# count of unique paths 
def countPaths(adj, k, maxn): 
  
    # An array that marks if 
    # the node is visited or not 
    vis = [False] * (maxn + 1
    ans = 0
  
    # Traverse till max value of node 
    for i in range(1, maxn+1): 
  
        # If not visited 
        if not vis[i]:
  
            # Get the number of 
            # nodes in that tree 
            numNodes = dfs(i, 0, adj, vis) 
  
            # Total unique paths in the current 
            # tree where numNodes is the total 
            # nodes in the tree 
            ans += numNodes * (numNodes - 1) // 2
          
    return ans 
  
# Function to add edges to 
# tree if value is less than K 
def addEdge(adj, u, v, k): 
  
    if u > k and v > k:
        adj[u].append(v) 
        adj[v].append(u) 
  
# Driver code 
if __name__ == "__main__":
  
    maxn = 12
  
    adj = [[] for i in range(maxn + 1)] 
    k = 3
  
    # Create undirected edges 
    addEdge(adj, 2, 11, k) 
    addEdge(adj, 2, 6, k) 
    addEdge(adj, 5, 11, k) 
    addEdge(adj, 5, 10, k) 
    addEdge(adj, 5, 12, k) 
    addEdge(adj, 6, 7, k) 
    addEdge(adj, 6, 8, k) 
  
    print(countPaths(adj, k, maxn))
  
# This code is contributed by Rituraj Jain

chevron_right


Output:

9


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rituraj_jain



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.