Created
October 30, 2012 19:25
-
-
Save SudhagarS/3982416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class EvenTree { | |
public static void main(String args[]){ | |
Scanner s=new Scanner(System.in); | |
int n,m; | |
int e1,e2; | |
n=s.nextInt(); | |
m=s.nextInt(); | |
Tree t=new Tree(n); | |
for(int i=0;i<m;i++){ | |
e1=s.nextInt(); | |
e2=s.nextInt(); | |
if (e1<e2) | |
t.addedge(e1,e2); | |
else | |
t.addedge(e2,e1); | |
} | |
t.count(); | |
System.out.println(t.removes); | |
} | |
} | |
class Node{ | |
int index; | |
ArrayList<Node> next=new ArrayList<Node>(); | |
} | |
class Tree{ | |
int n; | |
Node root; | |
Node[] nodes; | |
int removes,temp; | |
public Tree(int _n){ | |
n=_n; | |
nodes=new Node[n]; | |
for(int i=0;i<n;i++){ | |
nodes[i]=new Node(); | |
nodes[i].index=i+1; | |
} | |
root=nodes[0]; | |
} | |
void addedge(int a,int b){ | |
nodes[a-1].next.add(nodes[b-1]); | |
} | |
int count(){ | |
return countHelper(root); | |
} | |
int countHelper(Node node){ | |
int total =1; | |
for (Node next :node.next){ | |
temp=countHelper(next); | |
total+=temp; | |
if (temp%2==0 && temp>1) | |
removes+=1; | |
} | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment