Advertisement
Josif_tepe

Untitled

Mar 16th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. struct node{
  7. int idx;
  8. node(){
  9.  
  10. }
  11. node(int _idx){
  12. idx=_idx;
  13. }
  14. bool operator <(const node &tmp) const{
  15.     return idx > tmp.idx;
  16. }
  17.  
  18. };
  19. int main()
  20. {
  21.     int n;
  22.     int m;
  23.     cin>>n>>m;
  24.     vector<int>graph[n+5];
  25.     for(int i=0; i<m; i++){
  26.         int a;
  27.         int b;
  28.         cin>>a>>b;
  29.         graph[a].push_back(b);
  30.         graph[b].push_back(a);
  31.     }
  32.  
  33.     int s=1;
  34.     bool niza[n+5];
  35.     for(int i=0; i<=n; i++){
  36.         niza[i]=false;
  37.     }
  38.     niza[s]=true;
  39.     priority_queue<node>pq;
  40.     pq.push(node(s));
  41.     while(!pq.empty()){
  42.         node c=pq.top();
  43.         cout << c.idx << " ";
  44.         pq.pop();
  45.         niza[c.idx]=true;
  46.         for(int i=0; i < graph[c.idx].size(); i++){
  47.         int sosed=graph[c.idx][i];
  48.         if(niza[sosed]==false){
  49.         pq.push(node(sosed));
  50.         niza[sosed]=true;
  51.         }
  52.         }
  53.     }
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement