File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -491,6 +491,54 @@ func main() {
491491
492492### JavaScript
493493
494+ ``` javascript
495+ const rl = require (' readline' ).createInterface ({
496+ input: process .stdin ,
497+ output: process .stdout
498+ })
499+
500+ let inputLines = []
501+
502+ rl .on (' line' , (line )=> {
503+ inputLines .push (line)
504+ })
505+
506+ rl .on (' close' ,()=> {
507+ let [n , edgesCount]= inputLines[0 ].trim ().split (' ' ).map (Number )
508+
509+ let graph = Array .from ({length: n+ 1 } , ()=> {return []})
510+
511+ for (let i = 1 ; i < inputLines .length ; i++ ){
512+ let [from , to] = inputLines[i].trim ().split (' ' ).map (Number )
513+ graph[from].push (to)
514+ }
515+
516+ let visited = new Array (n + 1 ).fill (false )
517+
518+ let dfs = (graph , key , visited )=> {
519+ if (visited[key]){
520+ return
521+ }
522+
523+ visited[key] = true
524+ for (let nextKey of graph[key]){
525+ dfs (graph,nextKey , visited)
526+ }
527+ }
528+
529+ dfs (graph , 1 , visited)
530+
531+ for (let i = 1 ; i <= n;i++ ){
532+ if (visited[i] === false ){
533+ console .log (- 1 )
534+ return
535+ }
536+ }
537+ console .log (1 )
538+
539+ })
540+ ```
541+
494542### TypeScript
495543
496544### PhP
You can’t perform that action at this time.
0 commit comments