98. 所有可达路径
98. 所有可达路径 (kamacoder.com)
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class Main{
public static List<List<Integer>> res = new ArrayList<>();
public static List<Integer> path = new ArrayList<>();
public static void main (String[] args) {
/* code */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n+1][n+1];
for(int i = 1; i<=m; i++){
int start = sc.nextInt();
int end =sc.nextInt();
graph[start][end] = 1;
}
path.add(1);
dfs(graph,1,n);
if(res.size() ==0){
System.out.println(-1);
}
for(List<Integer> pa:res){
for(int i = 0; i<pa.size()-1;i++){
System.out.print(pa.get(i)+" ");
}
System.out.println(pa.get(pa.size()-1));
}
/*/
for(int i = 0; i<res.size(); i++){
for(int j = 0; j < res.get(i).size()-1; j++){
System.out.print(res.get(i).get(j)+" ");
}
System.out.println(res.get(i).get(res.get(i).size()-1));
}
//*/
/*/
for(int i=0;i<=n;i++){
for(int j = 0; j<=n;j++){
System.out.print(graph[i][j]);
}
System.out.println();
}
//*/
}
public static void dfs(int[][] graph, int x, int n){
if(x==n){
res.add(new ArrayList<>(path));
return;
}
for(int i = 1; i<=n; i++){
if(graph[x][i] == 1){
path.add(i);
dfs(graph,i,n);
path.remove(path.size()-1);
}
}
}
}