Even Tree

You are given a tree (a simple connected graph with no cycles). You have to remove as many edges from the tree as possible to obtain a forest with the condition that : Each connected component of the forest should contain an even number of vertices.

To accomplish this, you will remove some edges from the tree. Find out the number of removed edges.

Input Format
The first line of input contains two integers N and M. N is the number of vertices and M is the number of edges.
The next M lines contain two integers ui and vi which specifies an edge of the tree. (1-based index)

Output Format
Print the answer, a single integer.

Constraints
2 <= N <= 100.

Note: The tree in the input will be such that it can always be decomposed into components containing even number of nodes.

Sample Input

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8

Sample Output

2

Explanation
On removing edges (1, 3) and (1, 6), we can get the desired result.

Original tree:

Decomposed tree:

Copyright (c) 2015 HackerRank.
All Rights Reserved
Suggest Edits
5099 hackers have submitted code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100
typedef struct graph
    {
  int v;
  struct graph *e;  
}graph;
int visited[101];
int bfs[101];
int main() {
    int N,M,ui,vi,i,l,c,chld[101]={0},vrtx;
    graph *g[MAX],*next;
    scanf("%d%d",&N,&M);
    i=1;
    while(i<=N)
        {
        g[i]=(graph *)malloc(sizeof(graph));
        g[i]->v=i;
        g[i]->e=NULL;
        i++;
    }
    i=0;
    while(i<M)
        {
        scanf("%d%d",&ui,&vi);
        next=g[ui];
        while(next->e!=NULL)
            next=next->e;
        next->e = (graph *)malloc(sizeof(graph));
            next->e->v=vi;
            next->e->e=NULL;
        next=g[vi];
        while(next->e!=NULL)
            next=next->e;
        next->e = (graph *)malloc(sizeof(graph));
            next->e->v=ui;
            next->e->e=NULL;        
        i++;
    }
    i=0;
    l=1;
    bfs[0]=1;
    visited[0]=1;
    while(i<N)
        {
            next = g[bfs[i]];
            visited[next->v] = 1;
            chld[next->v] = 1;
                next=next->e;        
            while(next!=NULL)
                {
                if(visited[next->v] == 0)
                {
                   bfs[l++]=next->v;
                   chld[next->v] =1;
                }
                next=next->e;                
            }
        i++;
    }
    i=N-1;
    while(i>=0)
        {
            next = g[bfs[i]];
            visited[next->v] = 0;
            vrtx = next->v;
            next=next->e; 
        l=0;
            while(next!=NULL)
                {
                if(visited[next->v] == 1)
                {
                   chld[next->v] += chld[vrtx];                
                   l += 1;
                }
                next=next->e;                
            }
        if(l==0)
            chld[vrtx] -= 1;
        i--;
    }
    c=0;
    for(i=1;i<=N;i++)
        {
        if(chld[i] % 2 == 0)
            c+=1;  
    }
    printf("%d\n",c);
    return 0;
}

Compile and Run : http://ideone.com/VT1cZM
Follow On : https://twitter.com/codeifucansolve