Cod sursa(job #1641358)

Utilizator CostanMiriamCostan Miriam CostanMiriam Data 8 martie 2016 22:38:13
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <vector>

#define DIM 100010

using namespace std;

ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");

int n, m, solution;

vector<int> L[DIM];

pair<int, int> edges[DIM];

bool vis[DIM];

void DFS(int node) {

	vis[node] = true;

	for (int i = 0; i < L[node].size(); i++) {

		int child = L[node][i];

		if (vis[child])
			continue;

		DFS(child);
		edges[++solution] = make_pair(node, child);

	}

}

int main() {

	fin >> n >> m;

	for (int i = 1; i <= m; i++) {

		int x, y;

		fin >> x >> y;

		L[x].push_back(y);
		L[y].push_back(x);

	}

	DFS(1);

	if (solution != n - 1) {
		
		fout << "-1\n";
		
		return 0;
	
	}

	fout << 2 * solution << "\n";

	for (int i = 1; i <= solution; i++)
		fout << edges[i].second << " " << edges[i].first << "\n";
	
	for (int i = solution; i >= 1; i--)
		fout << edges[i].first << " " << edges[i].second << "\n";
	

	return 0;

}

//Miriam e tare!