fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long n,s,t,m,v,u,d,k;
  4. long long b[100068],trace[1000015];
  5. long long check[100015];
  6. vector<long long> a[100168];
  7. void DFS(long long u)
  8. {
  9. check[u]=1;
  10. for(int v:a[u])
  11. {if(check[v]==0)
  12. {
  13. trace[v]= u;
  14. DFS(v);
  15. }}
  16. }
  17. int main()
  18. {
  19. ios_base::sync_with_stdio(false);
  20. cin.tie(NULL); cout.tie(NULL);
  21. freopen("DFS.inp","r",stdin);
  22. freopen("DFS.out","w",stdout);
  23. cin>>n>>m>>s>>t;
  24. for(int i=1;i<=m;i++)
  25. {
  26. cin>>u>>v;
  27. a[u].push_back(v);
  28. }
  29.  
  30. for(int i=1;i<=n;i++)
  31. sort(a[i].begin(),a[i].end());
  32. fill(check,check+n,0);
  33. DFS(s);
  34. u=t;
  35. k=1;
  36. b[k]=t;
  37. while(u!=s)
  38. {
  39.  
  40. u= trace[u];
  41. k++;
  42. b[k]=u;
  43. }
  44. for(int i=k;i>=1;i--)
  45.  
  46. cout<<b[i]<<" ";
  47. }
  48.  
Success #stdin #stdout 0.01s 8980KB
stdin
Standard input is empty
stdout
Standard output is empty