You are viewing a single comment's thread. Return to all comments →
this is my final code all test cases passed except two if anyone can help me in it.
#include <bits/stdc++.h> using namespace std; vector<int> adj[100001]; long long pairs=0; int k; void dfs(int x,vector<int>parent){ parent.push_back(x); for(auto i:adj[x]){ int j=0; while(j<parent.size()){ if(abs(i-parent[j])<=k){ pairs++; } j++; } if(!adj[i].empty()){ dfs(i,parent); } } } int main() { int n; cin>>n; cin>>k; long long sum=(n*(n+1))/2; for(int i=0;i<n-1;i++){ int u,v; cin>>u>>v; sum-=v; adj[u].push_back(v); } int root=(int)sum; vector<int>parent; dfs(root,parent); cout<<pairs<<endl; return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Similar Pair
You are viewing a single comment's thread. Return to all comments →
this is my final code all test cases passed except two if anyone can help me in it.