Skip to content

Instantly share code, notes, and snippets.

@Rockbet
Created August 19, 2021 22:01
Show Gist options
  • Save Rockbet/3b767d0403264781a4ff69690b38a429 to your computer and use it in GitHub Desktop.
Save Rockbet/3b767d0403264781a4ff69690b38a429 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
vector<int> grafo[maxn];
vector<pair<int,int>> arestas;
set<pair<int,int>> usadas;
bool mark[maxn];
void dfs(int u){
mark[u] = 1;
for(int i=0; i<grafo[u].size(); i++){
int v = grafo[u][i];
if(mark[v] and usadas.find({u, v}) == usadas.end()){
arestas.push_back({u, v});
usadas.insert({u, v});
usadas.insert({v, u});
continue;
}
else if(mark[v]) continue;
usadas.insert({u, v});
usadas.insert({v, u});
dfs(v);
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n, m;
cin >> n >> m;
for(int i=1; i<=m; i++){
int u, v;
cin >> u >> v;
grafo[u].push_back(v);
grafo[v].push_back(u);
}
dfs(1);
cout << arestas.size() << "\n";
for(int i=0; i<arestas.size(); i++){
cout << arestas[i].first << " " << arestas[i].second << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment