Cod sursa(job #1372321)

Utilizator matei_cChristescu Matei matei_c Data 4 martie 2015 12:51:09
Problema Mesaj4 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<cmath>
using namespace std ;

#define maxn 1000005

int N, M ;

vector<int> graf[maxn] ;

bool sel[maxn] ;

vector< pair<int, int> > solutie ;

void dfs(int nod)
{
    sel[nod] = true ;

    for(size_t i = 0; i < graf[nod].size(); ++i)
    {
        int vecin = graf[nod][i] ;

        if( sel[vecin] == false )
        {
            solutie.push_back( make_pair(nod, vecin) ) ;
            dfs(vecin) ;
        }
    }
}

int main()
{
	std::ios_base::sync_with_stdio(false) ;

	freopen("mesaj4.in", "r", stdin);
	freopen("mesaj4.out", "w", stdout);

    cin >> N >> M ;

    for(int i = 1; i <= M; ++i)
    {
        int x, y ;
        cin >> x >> y ;

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

    dfs(1) ;

    if( solutie.size() < N - 1 )
    {
        cout << "-1" ;
        return 0 ;
    }

    int nrsol = solutie.size() ;

    cout << 2 * nrsol << "\n" ;

    for(int i = 0; i < nrsol; ++i)
    {
        int x = solutie[i].first ;
        int y = solutie[i].second ;;

        cout << x << " " << y << "\n" ;
    }

    for(int i = nrsol - 1; i >= 0; --i)
    {
        int x = solutie[i].first ;
        int y = solutie[i].second ;;

        cout << y << " " << x << "\n" ;
    }

	return 0 ;
}