-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoverIt.cpp
53 lines (49 loc) · 1.41 KB
/
CoverIt.cpp
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
/** https://codeforces.com/contest/1176/problem/E
#dfs #dsu #shortest-path
*/
#include<iostream>
#include<vector>
using namespace std;
int main() {
int testcases;
cin >> testcases;
for (int t=0; t < testcases; t++) {
int n, m;
cin >> n >> m;
int node, neighbour;
vector<vector<int> > graph(n+1);
for (int i=0; i < m; i++) {
cin >> node >> neighbour;
graph[node].push_back(neighbour);
graph[neighbour].push_back(node);
}
vector<int> chooseArr;
vector<int> escapeArr;
vector<bool> checkGraph(n+1);
for (int i=1; i < graph.size(); i++) {
if (!checkGraph[i]) {
chooseArr.push_back(i);
checkGraph[i] = true;
for (int adjacent : graph[i]) {
if (!checkGraph[adjacent]) {
escapeArr.push_back(adjacent);
checkGraph[adjacent] = true;
}
}
}
}
if (chooseArr.size() < escapeArr.size()) {
cout << chooseArr.size() << endl;
for ( int v : chooseArr){
cout << v << " ";
}
} else {
cout << escapeArr.size() << endl;
for ( int v : escapeArr){
cout << v << " ";
}
}
cout << endl;
}
return 0;
}