-
Notifications
You must be signed in to change notification settings - Fork 0
/
(D) Lunar New Year and a Wander
49 lines (47 loc) · 1.05 KB
/
(D) Lunar New Year and a Wander
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin>>n>>m;
vector<vector<int>>edges(m+1, vector<int>(2,0));
for(int i=0; i<m; i++)
{
int u, v;
cin>>u>>v;
edges.push_back({u,v});
}
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>>>pq;
for(auto it : edges)
{
pq.push({it[0], it[1]});
}
vector<int>adj[n+1];
while(!pq.empty())
{
adj[pq.top().first].push_back(pq.top().second);
adj[pq.top().second].push_back(pq.top().first);
pq.pop();
}
vector<int>vis(n+1, 0);
vis[1]=1;
priority_queue<int, vector<int>, greater<int>>q;
q.push(1);
vector<int>path(n+1, 0);
// cout<<1<<" ";
while(!q.empty())
{
int node = q.top();
cout<<node<<" ";
q.pop();
for(auto it : adj[node])
{
if(!vis[it])
{
// cout<<it<<" ";
q.push(it);
vis[it]=1;
}
}
}
return 0;
}