Cod sursa(job #1638035)

Utilizator Athena99Anghel Anca Athena99 Data 7 martie 2016 20:43:05
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");

const int nmax= 100000;

bool u[nmax+1];

int n, m, sol;
int solx[nmax+1], soly[nmax+1];

vector <int> v[nmax+1];

void dfs( int x ) {
    u[x]= 1;
    for ( vector <int>::iterator it= v[x].begin(); it!=v[x].end(); ++it ) {
        if ( u[*it]==0 ) {
            dfs(*it);

            ++sol;
            solx[sol]= x, soly[sol]= *it;
        }
    }
}

int main(  ) {
    fin>>n>>m;
    for ( int i= 1; i<=m; ++i ) {
        int x, y;
        fin>>x>>y;

        v[x].push_back(y);
        v[y].push_back(x);
    }

    dfs(1);

    if ( sol!=n-1 ) {
        fout<<"-1\n";
    } else {
        fout<<2*n-2<<"\n";
        for ( int i= 1; i<=sol; ++i ) {
            fout<<soly[i]<<" "<<solx[i]<<"\n";
        }
        for ( int i= sol; i>=1; --i ) {
            fout<<solx[i]<<" "<<soly[i]<<"\n";
        }
    }

    return 0;
}