Cod sursa(job #528209)

Utilizator rares192Preda Rares Mihai rares192 Data 2 februarie 2011 13:35:45
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include<fstream>
#include<vector>
#include<bitset>
using namespace std;

vector<vector<int> > a;
int n, m, nr;
bitset<100002> s;
vector<pair<int, int> > sol;

void read();
void DF(int );
void write();

int main()
{
	read();
	DF(1);
	write();
	return 0;
}

void read()
{
	ifstream fin("mesaj4.in");
	fin >> n >> m;
	
	a.resize(n + 1);
	int x, y;
	for(int i = 1; i <= m; ++i)
	{
		fin >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	
	fin.close();
}

void DF(int x)
{
	++nr;
	vector<int >::iterator it;
	s[x] = 1;
	
	for( it = a[x].begin(); it != a[x].end(); ++it)
		if( !s[ *it ] )
		{
			sol.push_back( make_pair(x, *it ) );
			DF( *it );
		}
}
	

void write()
{
	ofstream fout("mesaj4.out");
	
	vector<pair<int, int> >::iterator it; 
	
	if( nr < n)
		fout << -1;
	else
	{
		fout <<  2* (n - 1)  << '\n';
		for( it = sol.end() -1 ; it != sol.begin() - 1; --it)
			fout << it -> second <<" "<<it -> first <<'\n';
		
		for(it = sol.begin(); it != sol.end(); ++it)
			fout << it -> first <<" " <<it -> second <<'\n';
	}
}