Pagini recente » Cod sursa (job #913548) | Cod sursa (job #2958293) | Cod sursa (job #2988994) | Cod sursa (job #2056829) | Cod sursa (job #2274962)
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <fstream>
using namespace std;
ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");
#define MAXN 100005
int n, m;
vector<int> g[MAXN];
char v[MAXN];
vector<pair<int, int>> sol;
void dfsAquire(int x) {
v[x] = true;
for (auto y : g[x]) {
if (!v[y]) {
dfsAquire(y);
sol.push_back({ y,x });
}
}
}
int main() {
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfsAquire(1);
if (sol.size() < n - 1) {
fout << "-1";
return 0;
}
fout << (n - 1) * 2 << "\n";
for (auto x : sol) {
fout << x.first << " " << x.second << "\n";
}
for (int i = sol.size() - 1; i >= 0; i--) {
fout << sol[i].second << " " << sol[i].first << "\n";
}
return 0;
}