Pagini recente » Cod sursa (job #3255576) | Cod sursa (job #1143043) | Rating dan andrei (dan_paunul) | Profil FlorinGabrie1Haja | Cod sursa (job #2274944)
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
#define MAXN 100005
int n, m;
vector<int> g[MAXN];
char v[MAXN];
vector<pair<int, int>> sol;
struct Event {
int x, y, i;
};
stack<Event> s;
int main() {
freopen("mesaj4.in", "r", stdin);
freopen("mesaj4.out", "w", stdout);
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
s.push({ 1, 0, 0});
while (!s.empty()) {
auto x = s.top();
s.pop();
bool didPush = false;
v[x.x] = true;
for (int i = x.i; i < g[x.x].size(); ++i) {
auto y = g[x.x][i];
if (!v[y]) {
s.push({ x.x,x.y,i + 1 });
s.push({ y,x.x,0 });
didPush = true;
break;
}
}
if (!didPush) {
if (x.y != 0) {
sol.push_back({ x.x,x.y });
}
}
}
if (sol.size() < n - 1) {
printf("-1");
return 0;
}
printf("%d\n", (n - 1) * 2);
for (auto x : sol) {
printf("%d %d\n", x.first, x.second);
}
for (int i = sol.size() - 1; i >= 0; i--) {
printf("%d %d\n", sol[i].second, sol[i].first);
}
return 0;
}