orazma

a.cpp

Mar 27th, 2026 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, m;
  6. cin >> n >> m;
  7. int maxn = 105;
  8. vector<int> g[maxn];
  9.  
  10. for(int i=0; i<m; i++) {
  11. int x, y;
  12. cin >> x >> y;
  13. g[x].push_back(y);
  14. g[y].push_back(x);
  15. }
  16.  
  17. int start, end;
  18. cin >> start >> end;
  19.  
  20. queue<int> q;
  21. q.push(start);
  22. int dist[maxn];
  23. for(int i=1; i<=n; i++) {
  24. dist[i]=-1;
  25. }
  26. dist[start] = 0;
  27.  
  28. while(!q.empty()) {
  29. int v = q.front();
  30. q.pop();
  31.  
  32. for(int to : g[v]) {
  33. if(dist[to] == -1) {
  34. dist[to] = dist[v]+1;
  35. q.push(to);
  36. }
  37. }
  38. }
  39.  
  40. cout << dist[end];
  41. }
Advertisement
Add Comment
Please, Sign In to add comment