Cod sursa(job #465938)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 25 iunie 2010 15:25:45
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

const int NMAX=100001;

int N,M,t[NMAX];
vector<int> G[NMAX],Post;
ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");

void DF(int x)
{
	for (vector<int>::iterator it=G[x].begin();it!=G[x].end();++it)
	   if (t[*it]==-1)
	   {
		   t[*it]=x;
		   DF(*it);
	   }
	Post.push_back(x);
}

void DF2(int x)
{
	for (vector<int>::iterator it=G[x].begin();it!=G[x].end();++it)
	   if (t[*it]==x)
	   {
		   fout<<x<<" "<<*it<<"\n";
		   DF2(*it);
	   }
}


int main()
{
	int i,j;

	fin>>N>>M;
	while (M--)
	{
		fin>>i>>j;
		G[i].push_back(j);
		G[j].push_back(i);
	}
	
	memset(t,-1,sizeof(t));
	t[1]=0;
	DF(1);
	
	if ((int)Post.size() < N) {fout<<"-1";return 0;}
	
	fout<<2*(N-1)<<"\n";
	for (i=0;i<N-1;++i)
	  fout<<Post[i]<<" "<<t[Post[i]]<<"\n";
	  
	DF2(1);
	
	return 0;
}