Cod sursa(job #476954)

Utilizator dicu_dariaDaria Dicu dicu_daria Data 12 august 2010 20:44:44
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <stdio.h>
#include<cstring>
#include<fstream>

using namespace std;
typedef struct{
  int g,h;
}element;
element da[200002];
ofstream fo("mesaj4.out");
int ok=0;
int nr=0,vizitate=0,n, m,mm, viz[100005];
typedef struct{
int n1,n2;
} afisare;
afisare afis[100005];

typedef struct nod
{
	int x;
	nod *a;
} *pNod;
pNod v[100005];

void add(pNod &dest, int val)
{
	pNod p;
	p = new nod;
	p -> x = val;
	p -> a = dest;
	dest = p;
}

void citire()
{
	freopen("mesaj4.in","r",stdin);
	scanf("%d %d",&n,&m);
	int i, x, y;

	for (i = 1; i <= m; i++)
	{
		scanf("%d %d",&x,&y);
		add(v[x], y);
		add(v[y], x);
	}

}

void DFS(int nod,int precedent)
{
	pNod p;
	viz[nod] = 1;
	vizitate++;

	if(precedent!=0) {afis[++mm].n1=precedent; afis[mm].n2=nod; }
	for (p = v[nod]; p != NULL; p = p -> a) if (!viz[p -> x]) DFS(p -> x,nod);
    if(precedent!=0) {
      da[++nr].g=nod;
      da[nr].h=precedent;
    }

}

int main()
{
  int i;
	citire();
	DFS(1,0);
  if(vizitate!=n) {fo<<"-1\n"; return 0;
  }
  fo<<2*n-2<<"\n";
  for(i=1;i<=nr;i++)
  fo<<da[i].g<<" "<<da[i].h<<"\n";
	memset(viz,0,sizeof(viz));
	for(i=1;i<=mm;i++)
        fo<<afis[i].n1<<" "<<afis[i].n2<<"\n";
  fo.close();
	return 0;
}