Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <cmath>
- #include <climits>
- #include <iomanip>
- #include <queue>
- using namespace std;
- vector<vector<int>> Adj;
- int n, m, x, y,s , f, Trace[100001];
- vector <int> ans;
- queue<int> q;
- void read()
- {
- cin >> n >> m >> s >> f;
- Adj.resize(n + 1);
- for (int i = 1; i <= m; ++ i)
- {
- cin >> x >> y;
- Adj[x].push_back(y);
- }
- Trace[s] = -1;
- for (int i = 1; i <= n; ++ i)
- {
- sort(Adj[i].begin(), Adj[i].end());
- }
- }
- void DFS (int u)
- {
- for (int v : Adj[u])
- {
- if (Trace[v] == 0)
- {
- Trace[v] = u;
- DFS(v);
- }
- }
- }
- void solve()
- {
- DFS(s);
- while (f != s)
- {
- ans.push_back(f);
- f = Trace[f];
- }
- ans.push_back(s);
- reverse(ans.begin(), ans.end());
- for (int i : ans)
- {
- cout << i << ' ';
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- read();
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement