0

I am solving a simple question based on n-ary tree. Question is simple, finding total number of hand shaking between soldiers where each node of tree represents soldiers. Only ancestors nodes can handshake.

https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/practice-problems/algorithm/comrades-ii-6/

It’s the year 2552, the Humans just won a war against a very powerful alien race that had invaded our solar system. The Human army is in celebration mode!

The army has n soldiers. The soldiers are numbers from 1 to n. The army has a superiority hierarchy. Every soldier has one immediate superior. The superior of a superior of a soldier is also a superior to that soldier. So, a soldier may have one or more superiors but only one immediate superior.

Every soldier has to congratulate every other soldier. For a pair of soldiers if one of them is the superior of the other, they will shake hands. Otherwise, they will bump their fists.

You are given the list of immediate superior of all soldiers. Your job is to tell how many handshakes and fist bumps will be there.

My problem is that I have applied N-ary tree concept using map where int is key element storing parent and vector of int being its child, and applied DFS but after 2nd iteration my values changes, like:

     0                                             0
     |                                             |
     3                                             3
   /   \                                         /   \
  2     4                                       2      0    
 /                         changes to          /        
1                                             1

sample input:
1                                             //  ( test case)
4                                             //  ( number of nodes)
2 3 0 3                                       //  ( parent of node )


and due to this my code goes into loop (0 to 3 and then 3 to 0) and hence segmentation fault. I totally screwed up, tried everything but nothing happened.

Here is my code:


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
static long long int count1 = 0;

vector<int>::iterator itr;
map<int,vector<int> >tree;

void dfs(map<int,vector<int> > &tree, int node, int height){
    if(tree.find(node) == tree.end() ){
        return ;
    }
    if(tree[node].empty()){
        return ;
    }
    for(itr = tree.at(node).begin(); itr != tree.at(node).end(); itr++){    
        cout<<"node : "<<node <<"   child : "<<*itr<<endl;
        count1 += height;
        dfs(tree,*itr,height+1);
    }
}
int main(){
    int T,N,X;
    cin>>T;
    for(int i=0;i<T;i++){
        cin>>N;
        for(int j=1;j<=N;j++){
            cin>>X;
            tree[X].push_back(j);
        }

        dfs(tree,tree[0].front(),1);
        cout<<count1<<endl;
        count1 -= count1;
        tree.clear();
    }
    return 0;
}

I wanted to know why this is happening and where is my fault. What is problem in this approach? I know there is many more approaches but why is it failing?

c0der
  • 18,467
  • 6
  • 33
  • 65
Hritik Kumar
  • 89
  • 2
  • 6
  • 1
    Have you tried using a debugger? – eesiraed May 15 '18 at 04:51
  • yup,during recursion when it iterate from parent 3 to its child which is 2 and 4, and for next iteration from 2 as a parent to its child 1 ,it goes right but when it goes back to 3 for next child which is 4 it show 0 as its child not 4 due to which it again goes to 3 the 2 -> 1 and again 0 ->3 ->2 ->1 ; 0 ->3 ->2 ->1, no where 4 like 4 gets ommited – Hritik Kumar May 15 '18 at 05:10
  • 1
    Well, first off, I would stop using globals. BTW: Your question is off-topic, since it lacks a [mcve]. In particular, you should have removed the need for user input, both as a convenience to others and to rule out that the input is causing the problem. – Ulrich Eckhardt May 15 '18 at 05:13
  • i tried nothing happend – Hritik Kumar May 15 '18 at 05:16
  • Tried what? Already initially, you said you tried everything. If that was true, only the assumption that there is no deterministic behaviour would rationally justify the effort of continuing. – Ulrich Eckhardt May 15 '18 at 05:19
  • i just wanted to know why child of 3 is not showing 4 in function dfs(), so that next time when i implement this type of method ,it would not cause problem. – Hritik Kumar May 15 '18 at 05:23
  • 1
    Don't use global variables. Then you won't get into trouble with the fact that when you return from the recursive call to `dfs`, `itr` has been modified causing your loop to detour into limbo. – rici May 15 '18 at 05:53

0 Answers0