-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1176E.cpp
More file actions
66 lines (57 loc) · 1.36 KB
/
1176E.cpp
File metadata and controls
66 lines (57 loc) · 1.36 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
#define DEBUG
#include <bits/stdc++.h>
#define ll long long int
#define INF 200000000
using namespace std;
int vertex, edge;
vector<vector<int>> adj;
vector<int> ret;
void bfs(int index) {
ret = vector<int>(vertex+1, INF);
queue<int> q;
q.push(index);
// start bfs
while (!q.empty()) {
auto top = q.front();
q.pop();
for (auto to : adj[top]) {
if (ret[to] == INF) {
ret[to] = ret[top] + 1;
q.push(to);
}
}
}
}
int main() {
#ifdef DEBUG
freopen("in.in", "r", stdin);
#endif
int t, v1, v2;
cin >> t;
while (t--) {
cin >> vertex >> edge;
adj = vector<vector<int >>(vertex+1);
for (int i = 0; i < edge; ++i) {
cin >> v1 >> v2;
adj[v1].push_back(v2);
adj[v2].push_back(v1);
}
bfs(1);
vector<int> one, two;
for (int i = 1; i <= vertex; ++i) {
if (ret[i] & 1) one.push_back(i);
else two.push_back(i);
}
// output
if (one.size() < two.size()) {
cout << one.size() << endl;
for (auto v : one) cout << v << " ";
cout << endl;
} else {
cout << two.size() << endl;
for (auto v : two) cout << v << " ";
cout << endl;
}
}
return 0;
}