Cod sursa(job #471615)

Utilizator VmanDuta Vlad Vman Data 19 iulie 2010 20:05:29
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
using namespace std;
#include <cstdio>
#include <vector>

#define Nmax 100001

int N, M;
vector<int> G[Nmax];
int Q[Nmax], st, dr;
char W[Nmax];
int R[Nmax], L[Nmax];

int bfs(int S)
{
 int nod, k=0;
 vector<int> :: iterator it;
 
 W[ Q[st=dr=0] = S ] = 1;
 while (st<=dr)
       {
        nod = Q[st];
        for (it=G[nod].begin(); it<G[nod].end(); ++it)
            if (!W[*it])
               {
                R[++k] = *it;
                L[k] = nod;
                W[ Q[++dr] = R[k] ] = 1;
               }
        ++st;
       }
       
 return dr == N-1;
}

int main()
{
 int i, x, y;
 
 freopen("mesaj4.in","r",stdin);
 freopen("mesaj4.out","w",stdout);
 scanf("%d %d",&N,&M);
 for (i=0; i<M; ++i)
     {
      scanf("%d %d",&x,&y);
      G[x].push_back(y);
      G[y].push_back(x);
     }
 
 if (bfs(1))
    {
     printf("%d\n", (N-1)<<1);
     for (i=N-1; i>0; --i)
         printf("%d %d\n",R[i],L[i]);
     for (i=1; i<N; ++i)
         printf("%d %d\n",L[i],R[i]);
    }
    else printf("-1\n");
 
 return 0;
}