Pagini recente » Cod sursa (job #692602) | Cod sursa (job #540386) | Cod sursa (job #277278) | Cod sursa (job #400233) | Cod sursa (job #465390)
Cod sursa(job #465390)
#include <fstream>
#include <vector>
using namespace std;
const int MAXN = 100010;
int N, M;
vector <int> A[MAXN];
vector < pair <int, int> > Sol;
char u[MAXN];
void DFS(int nod, int root) {
u[nod] = 1;
for (size_t i = 0; i < A[nod].size(); ++i)
if (!u[A[nod][i]]) {
Sol.push_back(make_pair(nod, A[nod][i]));
DFS(A[nod][i], nod);
}
}
int main() {
ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");
fin >> N >> M;
for (int i = 1; i <= M; ++i) {
int x, y;
fin >> x >> y;
A[x].push_back(y);
A[y].push_back(x);
}
DFS(1, -1);
if ((int) Sol.size() != N-1) {
fout << -1 << '\n';
} else {
fout << 2*N-2 << '\n';
for (int i = N-2; i >= 0; --i)
fout << Sol[i].second << " " << Sol[i].first << '\n';
for (int i = 0; i < N-1; ++i)
fout << Sol[i].first << " " << Sol[i].second << '\n';
}
return 0;
}