Cod sursa(job #2274957)

Utilizator memecoinMeme Coin memecoin Data 2 noiembrie 2018 18:05:39
Problema Mesaj4 Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string.h>

using namespace std;

#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() {
	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);
	}

	dfsAquire(1);

	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;
}