File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ const readline = require ( 'readline' ) ;
2
+
3
+ const rl = readline . createInterface ( {
4
+ input : process . execArgv . includes ( "--stack-size=65536" ) ?
5
+ process . stdin : require ( 'fs' ) . createReadStream ( __dirname + '/input.txt' ) ,
6
+ output : process . stdout
7
+ } ) ;
8
+
9
+ let N ;
10
+ let graph ;
11
+ let dp ;
12
+ let visited ;
13
+
14
+ function dfs ( cur ) {
15
+ visited [ cur ] = true ;
16
+
17
+ for ( const child of graph [ cur ] ) {
18
+ if ( ! visited [ child ] ) {
19
+ dfs ( child ) ;
20
+ dp [ cur ] [ 0 ] += dp [ child ] [ 1 ] ;
21
+ dp [ cur ] [ 1 ] += Math . min ( ...dp [ child ] ) ;
22
+ }
23
+ }
24
+ }
25
+
26
+ rl . question ( '' , input => {
27
+ N = parseInt ( input ) ;
28
+ graph = Array . from ( { length : N + 1 } , ( ) => [ ] ) ;
29
+ dp = Array . from ( { length : N + 1 } , ( ) => [ 0 , 1 ] ) ;
30
+ visited = Array ( N + 1 ) . fill ( false ) ;
31
+
32
+ rl . on ( 'line' , line => {
33
+ const [ u , v ] = line . split ( ' ' ) . map ( Number ) ;
34
+ graph [ u ] . push ( v ) ;
35
+ graph [ v ] . push ( u ) ;
36
+ } ) . on ( 'close' , ( ) => {
37
+ dfs ( 1 ) ;
38
+ console . log ( Math . min ( ...dp [ 1 ] ) ) ;
39
+
40
+ process . exit ( ) ;
41
+ } ) ;
42
+ } ) ;
You can’t perform that action at this time.
0 commit comments