Cod sursa(job #846524)

Utilizator stoicatheoFlirk Navok stoicatheo Data 2 ianuarie 2013 13:07:46
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include<stdio.h>
#include<vector>

#define maxn 100005
#define pb push_back
#define fsize 1200000

using namespace std;

FILE*f=fopen("mesaj4.in","r");
FILE*g=fopen("mesaj4.out","w");

int n,m,i,x,y,k,p,u,nod,nodvcn,size,ch;
int C[maxn],moves1[maxn],moves2[maxn]; bool viz[maxn]; char buff[fsize+5];
vector<int>G[maxn];

inline void Bfs () {

    p = u = 1; C[1] = 1; viz[1] = 1;

    while ( p <= u ){
        nod = C[p];

        for ( i = 0 ; i < G[nod].size() ; ++i ){
            nodvcn = G[nod][i];
            if ( !viz[nodvcn] ){
                viz[nodvcn] = 1;
                C[++u] = nodvcn;
                ++k; moves1[k] = nodvcn; moves2[k] = nod;
            }
        }

        ++p;
    }
}

inline void solutie () {

    for ( i = 1 ; i <= n ; ++i ){
        if ( !viz[i] ){
            fprintf(g,"%d\n",-1);
            return ;
        }
    }

    fprintf(g,"%d\n",k<<1);
    for ( i = k ; i >= 1 ; --i ){
        fprintf(g,"%d %d\n",moves1[i],moves2[i]);
    }
    for ( i = 1 ; i <= k ; ++i ){
        fprintf(g,"%d %d\n",moves2[i],moves1[i]);
    }
}

inline int next () {
    int r = 0;

    while ( ch < size && buff[ch+1] >= '0' && buff[ch+1] <= '9' ){
        ++ch; r = r * 10 + buff[ch] - '0';
    }
    ++ch;
    return r;
}

int main () {

    size = fread(buff+1,1,fsize,f); ch = 0;
    n = next(); m = next();
    for ( i = 1 ; i <= m ; ++i ){
        x = next(); y = next();
        G[x].pb(y); G[y].pb(x);
    }

    Bfs();
    solutie();

    fclose(f);
    fclose(g);

    return 0;
}