forked from himanshu010/dsa-programming-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeforces 1106D.cpp
More file actions
105 lines (92 loc) · 1.91 KB
/
Copy pathcodeforces 1106D.cpp
File metadata and controls
105 lines (92 loc) · 1.91 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
*-----------------------------------------------------------*
| AUTHOR: Himanshu Aswal |
| ( website: himanshuaswal.com ) |
*-----------------------------------------------------------*
*/
#include <bits/stdc++.h>
#define moduli 998244353
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int, int>
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
using namespace std;
const int N = 100005, M = 22;
vector<vector<int>> graph(N);
vi ans;
vector<bool> visited(N);
void addEgde(int x, int y)
{
graph[x].pb(y);
graph[y].pb(x);
}
void sortVector()
{
for (int i = 0; i < N; ++i)
{
sort(graph[i].begin(), graph[i].end());
}
// for (auto x : graph[4]) {
// cout << x << " ";
// }
}
set<int> can;
void DFSfunction(int cur, int par, int n)
{
}
void solve()
{
int i, j, k, n, m, cnt = 0, sum = 0;
fill(visited.begin(), visited.end(), 0);
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
int x, y;
cin >> x >> y;
addEgde(x, y);
}
sortVector();
// DFSfunction(1, 0, n);
ans.pb(1);
visited[1] = 1;
while (ans.size() < n)
{
int i = ans.back();
for (auto nbr : graph[i])
{
if (visited[nbr] == 0)
{
can.insert(nbr);
}
}
auto it = can.begin();
ans.pb((*it));
visited[(*it)] = 1;
can.erase(it);
}
for (auto x : ans)
{
cout << x << " ";
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// int t;cin>>t;while(t--)
{
solve();
}
}