Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- struct node{
- int idx;
- node(){
- }
- node(int _idx){
- idx=_idx;
- }
- bool operator <(const node &tmp) const{
- return idx > tmp.idx;
- }
- };
- int main()
- {
- int n;
- int m;
- cin>>n>>m;
- vector<int>graph[n+5];
- for(int i=0; i<m; i++){
- int a;
- int b;
- cin>>a>>b;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- int s=1;
- bool niza[n+5];
- for(int i=0; i<=n; i++){
- niza[i]=false;
- }
- niza[s]=true;
- priority_queue<node>pq;
- pq.push(node(s));
- while(!pq.empty()){
- node c=pq.top();
- cout << c.idx << " ";
- pq.pop();
- niza[c.idx]=true;
- for(int i=0; i < graph[c.idx].size(); i++){
- int sosed=graph[c.idx][i];
- if(niza[sosed]==false){
- pq.push(node(sosed));
- niza[sosed]=true;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement