1- //pakage joney_000[let_me_start]
2- //
31import java .util .*;
42import java .lang .*;
53import java .io .*;
64import java .math .*;
75/*
8- * Author : joney_000[let_me_start]
9- * Algorithm : N/A
10- * Platform : N/A
11- *
6+ * Author : joney_000[developer.jaswant@gmail.com]
7+ * Algorithm : DFS : depth first search in Liner Time and Space
8+ * Platform : Codeforces
129 */
1310
11+ class A {
1412
15- /* The Main Class */
16- class A
17- {
1813 private InputStream inputStream ;
1914 private OutputStream outputStream ;
2015 private FastReader in ;
@@ -59,8 +54,8 @@ void run()throws Exception{
5954 adj [u ].add (v );
6055 adj [v ].add (u );
6156 }
62- LinkedList <Integer > adj1 [] = getCopy (adj , n ); // wow
63- dfs (adj1 , 1 , n ); //Assuming that node 1 is the root node
57+ LinkedList <Integer > adj0 [] = getCopy (adj , n ); // wow
58+ dfs (adj0 , 1 , n ); //Assuming that node 1 is the root node
6459 long ans = 0 ;
6560 out .write ("" +ans +"\n " );
6661
@@ -70,59 +65,55 @@ void once(){
7065
7166 }
7267
73- int MAX_N = 200005 ;
74- int level [] = new int [MAX_N + 1 ];
75- int f [] = new int [MAX_N + 1 ]; // f[i] = father of i
76- LinkedList <Integer > adj [] = new LinkedList [MAX_N + 1 ];
77-
68+ int MAXN = 200005 ;
69+ int depth [] = new int [MAXN + 1 ];
70+ int f [] = new int [MAXN + 1 ]; // f[i] = parent of i
71+ LinkedList <Integer > adj [] = new LinkedList [MAXN + 1 ];
72+ boolean vis [] = new boolean [MAXN +1 ];
73+
7874 void clear (){
79- for (int i = 1 ; i <= MAX_N ; i ++){
75+ for (int i = 1 ; i <= MAXN ; i ++){
8076 adj [i ] = new LinkedList <Integer >();
8177 }
8278 }
8379
84- // Maintain mutability
85- LinkedList <Integer >[] getCopy (LinkedList <Integer > adj [] , int n )throws Exception {
86- LinkedList <Integer > adj_copy [] = new LinkedList [n + 1 ];
80+ // Maintain immutability
81+ LinkedList <Integer >[] getCopy (LinkedList <Integer >[] adj , int n )throws Exception {
82+ LinkedList <Integer > adjCopy [] = new LinkedList [n + 1 ];
8783 for (int i = 1 ; i <= n ; i ++){
88- adj_copy [i ] = new LinkedList <Integer >();
84+ adjCopy [i ] = new LinkedList <Integer >();
8985 for (int x : adj [i ]){
90- adj_copy [i ].add (x );
86+ adjCopy [i ].add (x );
9187 }
9288 }
93- return adj_copy ;
89+ return adjCopy ;
9490 }
9591
9692 void dfs (LinkedList <Integer > adj [], int root , int n )throws Exception {
9793
98- boolean vis [] = new boolean [n +1 ];
99- LinkedList <Integer > q = new LinkedList <Integer >();
100- int index = 1 ;
101- int l = 0 ; //level
102-
103- q .add (root );
94+ LinkedList <Integer > queue = new LinkedList <Integer >();
95+ int currentDepth = 0 ; //level
96+ queue .add (root );
10497 vis [root ] = true ;
10598
106- while (!q .isEmpty ()){
99+ while (!queue .isEmpty ()){
107100
108- int u = q .getLast (); // The Stack
109- level [u ] = l ;
101+ int u = queue .getLast (); // The Stack
102+ depth [u ] = currentDepth ;
110103
111104 if (adj [u ].size ()>0 ){
112105 int v = adj [u ].removeFirst ();
113106 if (!vis [v ]){
114- q .add (v );
115- l ++;
107+ queue .add (v );
108+ currentDepth ++;
116109 vis [v ] = true ;
117- level [v ] = l ;
110+ depth [v ] = currentDepth ;
118111 // f[v] = u;
119112 }
120-
121113 }else {
122- int v = q .removeLast ();
123- l --;
114+ int v = queue .removeLast ();
115+ currentDepth --;
124116 }
125-
126117 }
127118 }
128119 //****************************** Gerenal Utilities ***********************//
0 commit comments