Pagini recente » Cod sursa (job #230923) | Cod sursa (job #70489) | Cod sursa (job #2535606) | Cod sursa (job #379658) | Cod sursa (job #1400809)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("mesaj4.in");
ofstream out("mesaj4.out");
const int NMAX = 100000;
struct DAP {
int x,y;
};
DAP make_DAP( int x, int y ) {
DAP S;
S.x = x;
S.y = y;
return S;
}
vector <int> G[NMAX+1];
vector <DAP> sol;
int N,M, vizitati = 0;
bool viz[NMAX+1];
void DFS( int nod ) {
++vizitati;
viz[nod] = 1;
for( int i = 0; i < (int)G[nod].size(); ++i ) {
int x = G[nod][i];
if( !viz[x] ) {
sol.push_back( make_DAP(x, nod) );
DFS( x );
}
}
}
int main() {
in >> N >> M;
for( int i = 1; i <= M; ++i ) {
int x,y;
in >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
DFS(1);
int sz = (int)sol.size();
if( sz < N - 1 ) {
out << "-1\n";
return 0;
}
out << 2*sz << '\n';
for( int i = 0; i < sz; ++i ) {
out << sol[i].x << ' ' << sol[i].y << '\n';
}
for( int i = sz-1; i >= 0; --i ) {
out << sol[i].y << ' ' << sol[i].x << '\n';
}
return 0;
}