Cod sursa(job #2254872)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 6 octombrie 2018 09:25:16
Problema Mesaj4 Scor 0
Compilator cpp Status done
Runda problemioare Marime 0.81 kb
#include <vector>
#include <fstream>

using namespace std;

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

const int Dim = 1e5;

using VI = vector< int >;
using VVI = vector < VI > ;

VVI G;
vector < pair < int, int > > Sol;
int n,m,sol;
bool Viz[Dim];
void Dfs(int x) ;

int main() {
	
	fin >> n >> m;
	G = VVI ( n + 1);
	int x,y;
	for (; m > 0; --m) {
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	Dfs(1);
	if ( sol < n - 1) {
		fout << -1;
		return 0;
	}
	fout << sol * 2 << "\n";
	for ( const auto & i : Sol)
		fout << i.first <<" " << i.second << "\n";
	for( int i = Sol.size() - 1; i >= 1; --i)
		fout << Sol[i].second << " " << Sol[i].first << "\n";
}


void Dfs(int x) {
	
	Viz[x] = 1;
	for ( const int & y : G[x] )	
		if ( !Viz[y] ) {
			++sol;
			Sol.push_back({x,y});
			Dfs(y);
		}
}