Skip to content

Commit 95a0671

Browse files
authored
Create Root the Tree.java
1 parent d47f694 commit 95a0671

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
5+
class Codechef
6+
{
7+
static class FastReader
8+
{
9+
BufferedReader br;
10+
StringTokenizer st;
11+
12+
public FastReader()
13+
{
14+
br = new BufferedReader(new
15+
InputStreamReader(System.in));
16+
}
17+
18+
String next()
19+
{
20+
while (st == null || !st.hasMoreElements())
21+
{
22+
try
23+
{
24+
st = new StringTokenizer(br.readLine());
25+
}
26+
catch (IOException e)
27+
{
28+
e.printStackTrace();
29+
}
30+
}
31+
return st.nextToken();
32+
}
33+
34+
int nextInt()
35+
{
36+
return Integer.parseInt(next());
37+
}
38+
39+
long nextLong()
40+
{
41+
return Long.parseLong(next());
42+
}
43+
44+
double nextDouble()
45+
{
46+
return Double.parseDouble(next());
47+
}
48+
49+
String nextLine()
50+
{
51+
String str = "";
52+
try
53+
{
54+
str = br.readLine();
55+
}
56+
catch (IOException e)
57+
{
58+
e.printStackTrace();
59+
}
60+
return str;
61+
}
62+
}
63+
public static void main (String[] args) throws java.lang.Exception
64+
{
65+
// your code goes here
66+
FastReader sc=new FastReader();
67+
int t=sc.nextInt();
68+
while(t-->0) {
69+
int n=sc.nextInt();
70+
int[] fr=new int[n-1];
71+
int[] to=new int[n-1];
72+
for(int i=0;i<n-1;i++) {
73+
fr[i]=sc.nextInt();
74+
to[i]=sc.nextInt();
75+
}
76+
Dictionary d= new Hashtable();
77+
int temp;
78+
for(int i=0;i<n-1;i++) {
79+
if(d.get(to[i])==null) {
80+
d.put(to[i],1);
81+
}
82+
else {
83+
temp=(int) d.get(to[i]);
84+
d.put(to[i],temp+1);
85+
}
86+
}
87+
int s=0;
88+
for (Enumeration i = d.elements(); i.hasMoreElements();)
89+
{
90+
temp=(int) i.nextElement();
91+
if(temp>1) {
92+
s=s+temp-1;
93+
}
94+
}
95+
System.out.println(s);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)