Pagini recente » Cod sursa (job #3130682) | Cod sursa (job #1934842) | Cod sursa (job #2256375) | Cod sursa (job #1846929) | Cod sursa (job #1372321)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<cmath>
using namespace std ;
#define maxn 1000005
int N, M ;
vector<int> graf[maxn] ;
bool sel[maxn] ;
vector< pair<int, int> > solutie ;
void dfs(int nod)
{
sel[nod] = true ;
for(size_t i = 0; i < graf[nod].size(); ++i)
{
int vecin = graf[nod][i] ;
if( sel[vecin] == false )
{
solutie.push_back( make_pair(nod, vecin) ) ;
dfs(vecin) ;
}
}
}
int main()
{
std::ios_base::sync_with_stdio(false) ;
freopen("mesaj4.in", "r", stdin);
freopen("mesaj4.out", "w", stdout);
cin >> N >> M ;
for(int i = 1; i <= M; ++i)
{
int x, y ;
cin >> x >> y ;
graf[x].push_back(y) ;
graf[y].push_back(x) ;
}
dfs(1) ;
if( solutie.size() < N - 1 )
{
cout << "-1" ;
return 0 ;
}
int nrsol = solutie.size() ;
cout << 2 * nrsol << "\n" ;
for(int i = 0; i < nrsol; ++i)
{
int x = solutie[i].first ;
int y = solutie[i].second ;;
cout << x << " " << y << "\n" ;
}
for(int i = nrsol - 1; i >= 0; --i)
{
int x = solutie[i].first ;
int y = solutie[i].second ;;
cout << y << " " << x << "\n" ;
}
return 0 ;
}