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 4f9c892

Browse files
Merge pull request youngyangyang04#2596 from imiss-U/master
Update 0098.所有可达路径.md
2 parents b7eb403 + c4eb2cb commit 4f9c892

File tree

1 file changed

+178
-1
lines changed

1 file changed

+178
-1
lines changed

‎problems/kamacoder/0098.所有可达路径.md‎

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,186 @@ int main() {
422422
## 其他语言版本
423423

424424
### Java
425+
#### 邻接矩阵写法
426+
```java
427+
import java.util.ArrayList;
428+
import java.util.List;
429+
import java.util.Scanner;
430+
431+
public class Main {
432+
static List<List<Integer>> result = new ArrayList<>(); // 收集符合条件的路径
433+
static List<Integer> path = new ArrayList<>(); // 1节点到终点的路径
434+
435+
public static void dfs(int[][] graph, int x, int n) {
436+
// 当前遍历的节点x 到达节点n
437+
if (x == n) { // 找到符合条件的一条路径
438+
result.add(new ArrayList<>(path));
439+
return;
440+
}
441+
for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点
442+
if (graph[x][i] == 1) { // 找到 x链接的节点
443+
path.add(i); // 遍历到的节点加入到路径中来
444+
dfs(graph, i, n); // 进入下一层递归
445+
path.remove(path.size() - 1); // 回溯,撤销本节点
446+
}
447+
}
448+
}
425449

426-
### Python
450+
public static void main(String[] args) {
451+
Scanner scanner = new Scanner(System.in);
452+
int n = scanner.nextInt();
453+
int m = scanner.nextInt();
454+
455+
// 节点编号从1到n,所以申请 n+1 这么大的数组
456+
int[][] graph = new int[n + 1][n + 1];
457+
458+
for (int i = 0; i < m; i++) {
459+
int s = scanner.nextInt();
460+
int t = scanner.nextInt();
461+
// 使用邻接矩阵表示无向图,1 表示 s 与 t 是相连的
462+
graph[s][t] = 1;
463+
}
464+
465+
path.add(1); // 无论什么路径已经是从1节点出发
466+
dfs(graph, 1, n); // 开始遍历
467+
468+
// 输出结果
469+
if (result.isEmpty()) System.out.println(-1);
470+
for (List<Integer> pa : result) {
471+
for (int i = 0; i < pa.size() - 1; i++) {
472+
System.out.print(pa.get(i) + " ");
473+
}
474+
System.out.println(pa.get(pa.size() - 1));
475+
}
476+
}
477+
}
478+
```
479+
480+
#### 邻接表写法
481+
```java
482+
import java.util.ArrayList;
483+
import java.util.LinkedList;
484+
import java.util.List;
485+
import java.util.Scanner;
486+
487+
public class Main {
488+
static List<List<Integer>> result = new ArrayList<>(); // 收集符合条件的路径
489+
static List<Integer> path = new ArrayList<>(); // 1节点到终点的路径
490+
491+
public static void dfs(List<LinkedList<Integer>> graph, int x, int n) {
492+
if (x == n) { // 找到符合条件的一条路径
493+
result.add(new ArrayList<>(path));
494+
return;
495+
}
496+
for (int i : graph.get(x)) { // 找到 x指向的节点
497+
path.add(i); // 遍历到的节点加入到路径中来
498+
dfs(graph, i, n); // 进入下一层递归
499+
path.remove(path.size() - 1); // 回溯,撤销本节点
500+
}
501+
}
502+
503+
public static void main(String[] args) {
504+
Scanner scanner = new Scanner(System.in);
505+
int n = scanner.nextInt();
506+
int m = scanner.nextInt();
507+
508+
// 节点编号从1到n,所以申请 n+1 这么大的数组
509+
List<LinkedList<Integer>> graph = new ArrayList<>(n + 1);
510+
for (int i = 0; i <= n; i++) {
511+
graph.add(new LinkedList<>());
512+
}
513+
514+
while (m-- > 0) {
515+
int s = scanner.nextInt();
516+
int t = scanner.nextInt();
517+
// 使用邻接表表示 s -> t 是相连的
518+
graph.get(s).add(t);
519+
}
427520

521+
path.add(1); // 无论什么路径已经是从1节点出发
522+
dfs(graph, 1, n); // 开始遍历
523+
524+
// 输出结果
525+
if (result.isEmpty()) System.out.println(-1);
526+
for (List<Integer> pa : result) {
527+
for (int i = 0; i < pa.size() - 1; i++) {
528+
System.out.print(pa.get(i) + " ");
529+
}
530+
System.out.println(pa.get(pa.size() - 1));
531+
}
532+
}
533+
}
534+
```
535+
### Python
536+
#### 邻接矩阵写法
537+
``` python
538+
def dfs(graph, x, n, path, result):
539+
if x == n:
540+
result.append(path.copy())
541+
return
542+
for i in range(1, n + 1):
543+
if graph[x][i] == 1:
544+
path.append(i)
545+
dfs(graph, i, n, path, result)
546+
path.pop()
547+
548+
def main():
549+
n, m = map(int, input().split())
550+
graph = [[0] * (n + 1) for _ in range(n + 1)]
551+
552+
for _ in range(m):
553+
s, t = map(int, input().split())
554+
graph[s][t] = 1
555+
556+
result = []
557+
dfs(graph, 1, n, [1], result)
558+
559+
if not result:
560+
print(-1)
561+
else:
562+
for path in result:
563+
print(' '.join(map(str, path)))
564+
565+
if __name__ == "__main__":
566+
main()
567+
```
568+
569+
#### 邻接表写法
570+
``` python
571+
from collections import defaultdict
572+
573+
result = [] # 收集符合条件的路径
574+
path = [] # 1节点到终点的路径
575+
576+
def dfs(graph, x, n):
577+
if x == n: # 找到符合条件的一条路径
578+
result.append(path.copy())
579+
return
580+
for i in graph[x]: # 找到 x指向的节点
581+
path.append(i) # 遍历到的节点加入到路径中来
582+
dfs(graph, i, n) # 进入下一层递归
583+
path.pop() # 回溯,撤销本节点
584+
585+
def main():
586+
n, m = map(int, input().split())
587+
588+
graph = defaultdict(list) # 邻接表
589+
for _ in range(m):
590+
s, t = map(int, input().split())
591+
graph[s].append(t)
592+
593+
path.append(1) # 无论什么路径已经是从1节点出发
594+
dfs(graph, 1, n) # 开始遍历
595+
596+
# 输出结果
597+
if not result:
598+
print(-1)
599+
for pa in result:
600+
print(' '.join(map(str, pa)))
601+
602+
if __name__ == "__main__":
603+
main()
604+
```
428605
### Go
429606

430607
### Rust

0 commit comments

Comments
(0)

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