Advertisement
DuongNhi99

LEXIPATH

Mar 10th, 2022
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int INF = 1e9 + 7;
  5. const int N = 105;
  6.  
  7. int n, m, s, t;
  8. vector<int> graph[N];
  9.  
  10. int visited[N];
  11. int parent[N];
  12.  
  13. void DFS(int u) {
  14.     visited[u] = true;
  15.    
  16.     for (int v : graph[u]) {
  17.         if (!visited[v]) {
  18.             parent[v] = u;
  19.             visited[v] = true;
  20.             DFS(v);
  21.         }
  22.     }
  23. }
  24.  
  25. vector<int> ans;
  26. void Trace(int s, int t) {
  27.     ans.push_back(t);
  28.  
  29.     int u = parent[t];
  30.     if (s == u) {
  31.         ans.push_back(s);
  32.         return;
  33.     }
  34.    
  35.     Trace(s, u);
  36. }
  37.  
  38. void solve() {
  39.     cin >> n >> m >> s >> t;
  40.     for (int i = 1; i <= m; ++i) {
  41.         int u, v; cin >> u >> v;
  42.         graph[u].push_back(v);
  43.         graph[v].push_back(u);
  44.     }
  45.    
  46.     for (int i = 1; i <= n; ++i)
  47.         sort(graph[i].begin(), graph[i].end());
  48.     iota(parent + 1, parent + n + 1, 1);
  49.    
  50.     DFS(s);
  51.     Trace(s, t);
  52.    
  53.     reverse(ans.begin(), ans.end());
  54.     for (int u : ans)
  55.         cout << u << ' ';
  56.    
  57.    
  58.     return;
  59. }
  60.  
  61. int main() {
  62.     ios_base::sync_with_stdio(false);
  63.     cin.tie(NULL);
  64.  
  65.     solve();
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement