1
- //pakage joney_000[let_me_start]
2
- //
3
1
import java .util .*;
4
2
import java .lang .*;
5
3
import java .io .*;
6
4
import java .math .*;
7
5
/*
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
12
9
*/
13
10
11
+ class A {
14
12
15
- /* The Main Class */
16
- class A
17
- {
18
13
private InputStream inputStream ;
19
14
private OutputStream outputStream ;
20
15
private FastReader in ;
@@ -59,8 +54,8 @@ void run()throws Exception{
59
54
adj [u ].add (v );
60
55
adj [v ].add (u );
61
56
}
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
64
59
long ans = 0 ;
65
60
out .write ("" +ans +"\n " );
66
61
@@ -70,59 +65,55 @@ void once(){
70
65
71
66
}
72
67
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
+
78
74
void clear (){
79
- for (int i = 1 ; i <= MAX_N ; i ++){
75
+ for (int i = 1 ; i <= MAXN ; i ++){
80
76
adj [i ] = new LinkedList <Integer >();
81
77
}
82
78
}
83
79
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 ];
87
83
for (int i = 1 ; i <= n ; i ++){
88
- adj_copy [i ] = new LinkedList <Integer >();
84
+ adjCopy [i ] = new LinkedList <Integer >();
89
85
for (int x : adj [i ]){
90
- adj_copy [i ].add (x );
86
+ adjCopy [i ].add (x );
91
87
}
92
88
}
93
- return adj_copy ;
89
+ return adjCopy ;
94
90
}
95
91
96
92
void dfs (LinkedList <Integer > adj [], int root , int n )throws Exception {
97
93
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 );
104
97
vis [root ] = true ;
105
98
106
- while (!q .isEmpty ()){
99
+ while (!queue .isEmpty ()){
107
100
108
- int u = q .getLast (); // The Stack
109
- level [u ] = l ;
101
+ int u = queue .getLast (); // The Stack
102
+ depth [u ] = currentDepth ;
110
103
111
104
if (adj [u ].size ()>0 ){
112
105
int v = adj [u ].removeFirst ();
113
106
if (!vis [v ]){
114
- q .add (v );
115
- l ++;
107
+ queue .add (v );
108
+ currentDepth ++;
116
109
vis [v ] = true ;
117
- level [v ] = l ;
110
+ depth [v ] = currentDepth ;
118
111
// f[v] = u;
119
112
}
120
-
121
113
}else {
122
- int v = q .removeLast ();
123
- l --;
114
+ int v = queue .removeLast ();
115
+ currentDepth --;
124
116
}
125
-
126
117
}
127
118
}
128
119
//****************************** Gerenal Utilities ***********************//
0 commit comments