Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n;
- vector<bool> visited;
- vector<int> red; // even
- vector<int> blue; // odd
- vector<vector<int>> graph;
- void dfs(int source, int counter){
- if (visited[source]){
- return;
- }
- else{
- visited[source] = true;
- if (counter % 2 == 0){
- red.push_back(source);
- }
- else{
- blue.push_back(source);
- }
- for (int child : graph[source]){
- if (!visited[child]){
- dfs(child, (counter + 1)%2);
- }
- }
- }
- }
- int main(){
- int t;
- cin >> t;
- while(t--){
- int n, m;
- cin >> n >> m;
- for (int i = 0; i < m; i++){
- int u, v;
- cin >> u >> v;
- }
- dfs(0, 0);
- int red_nodes = red.size();
- int blue_nodes = blue.size();
- cout << min(red_nodes, blue_nodes) << endl;
- if (blue_nodes <= red_nodes){
- for (auto i : blue){
- cout << i << " ";
- }
- }
- else{
- for (auto i : red){
- cout << i << " ";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement