Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- vector<int> v[100];
- int vis[100] = {0};
- vector<int> v1;
- void dfs(int x)
- {
- v1.push_back(x);
- cout << x << ": " << endl;
- int s = v[x].size();
- vis[x] = 1;
- for (int i = 0; i < s; i++)
- {
- if (!vis[v[x][i]])
- {
- dfs(v[x][i]);
- v1.push_back(x);
- cout << "return to callee: " << x
- << endl;
- }
- }
- }
- // void dfs(int at)
- // {
- // if (vis[at])
- // return;
- // vis[at] = 1;
- // v1.push_back(at);
- // for (int i = 0; i < v[at].size(); i++)
- // {
- // if (!vis[v[at][i]])
- // {
- // //cout << v[at][i] << " ";
- // dfs(v[at][i]);
- // v1.push_back(at);
- // }
- // }
- // //cout << endl;
- // }
- int main()
- {
- int node, edge;
- cout << "all adjacent path from source 1 --> destination: " << endl;
- cin >> node >> edge;
- int s, d;
- for (int i = 0; i < edge; i++)
- {
- cin >> s >> d;
- v[s].push_back(d);
- v[d].push_back(s);
- }
- for (int i = 1; i <= node; i++)
- {
- cout << i << "--> ";
- for (int j = 0; j < v[i].size(); j++)
- {
- cout << v[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- dfs(1);
- cout << "final adj path: " << endl;
- auto itr = v1.begin();
- for (; itr != v1.end(); itr++)
- {
- cout << *itr << " ";
- }
- return 0;
- }
- // 8 9
- // 1 3
- // 1 4
- // 2 5
- // 2 6
- // 3 4
- // 4 5
- // 6 6
- // 5 7
- // 7 7
- // 1 7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement