Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a8382d9

Browse files
Merge pull request youngyangyang04#2832 from liaoming10/master
添加:0105.有向图的完全可达性Javascript代码
2 parents f86004e + 0f152be commit a8382d9

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎problems/kamacoder/0105.有向图的完全可达性.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /