// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // DFS function to traverse the tree and find // number of special nodes void dfs(int val[], int n, vector<int> adj[], int v, unordered_set<int>& values, int& ans) { // If value of current node is already // present earlier in path then this // node and all other nodes connected to // it are not special if (values.count(val[v])) return; // Insert value of current node in // set of values traversed ans++; values.insert(val[v]); // Call dfs on all adjacent nodes for (auto ele : adj[v]) { dfs(val, n, adj, ele, values, ans); } // Erase value of current node as all paths // passing through current node are traversed values.erase(val[v]); } // Driver code int main() { int val[] = { 0, 2, 1, 4, 3, 4, 8, 10, 2, 5, 1 }; int n = sizeof(val) / sizeof(val[0]); vector<int> adj[n]; adj[1].push_back(2); adj[1].push_back(3); adj[2].push_back(4); adj[2].push_back(5); adj[2].push_back(6); adj[3].push_back(7); adj[5].push_back(8); adj[5].push_back(9); adj[5].push_back(10); unordered_set<int> values; int ans = 0; dfs(val, n, adj, 1, values, ans); cout << ans; return 0; }