题干:
代码:
#include <iostream>
#include <vector>
using namespace std;vector<vector<int>>res;
vector<int>path;void dfs(vector<vector<int>>&graph, int x, int n)
{if(x == n){res.push_back(path);return;//}for(int i = 1; i <= n; i++)//一定要扫描到n{if(graph[x][i] == 1){path.push_back(i);dfs(graph, i, n);//以i为起点继续path.pop_back();}}
}int main()
{int n, m, s, t;cin>>n>>m;vector<vector<int>>graph(n + 1, vector<int>(n + 1, 0));//1~n需要申请n+1个空间while(m--){cin>>s>>t;graph[s][t] = 1;}path.push_back(1);//无论什么路径都是从0节点开始dfs(graph, 1, n);//开始遍历if(res.size() == 0)cout<<-1<<endl;//for(vector<int> &tmp : res){for(int i = 0; i < tmp.size() - 1; i++)//为了保证最后一个数后面没有空格,所以扫到倒数第二个{cout<<tmp[i]<<" ";}cout<<tmp[tmp.size() - 1]<<endl;}}