Cod sursa(job #2274962)

Utilizator memecoinMeme Coin memecoin Data 2 noiembrie 2018 18:11:16
Problema Mesaj4 Scor 100
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>
#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;
}