Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int maxN = 1000;
- vector<int> adj[maxN];
- bool visited[maxN];
- int parent[maxN];
- void dfs (int node,int par) {
- visited[node] = true;
- parent[node] = par;
- for (int child : adj[node]) {
- if (visited[child] == false) {
- dfs(child,node);
- }
- }
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- cin >> n >> m;
- for (int i = 1; i <= m; ++i) {
- int u,v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- dfs(1,-1); //-1 is a termination point
- int dest = 6;
- vector<int> path;
- path.push_back(dest);
- while (parent[dest] != source) {
- dest = parent[dest];
- path.push_back(dest);
- }
- reverse(path.begin(),path.end());
- for (int p : path) cout << p << ' ';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement