Cod sursa(job #1993625)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 23 iunie 2017 13:44:31
Problema Mesaj4 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax = 1e5;

bool viz[nmax + 1];
vector< int > g[nmax + 1];
vector< pair<int, int> > trim;

void dfs (int nod) {
    viz[ nod ] = 1;

    for (auto i : g[ nod ]) {
        if (viz[ i ] == 0) {
            trim.push_back(make_pair(nod, i));
            dfs( i );
        }
    }
}

int main() {
    int n, m;
    fin >> n >> m;

    for (int i = 1; i <= m; ++ i) {
        int x, y;
        fin >> x >> y;
        g[ x ].push_back( y );
        g[ y ].push_back( x );
    }

    dfs( 1 );

    fout << 2 * (n - 1) << "\n";
    for (int i = (int)trim.size() - 1; i >= 0; -- i) {
        fout << trim[ i ].second << " " << trim[ i ].first << "\n";
    }
    for (auto i : trim) {
        fout << i.first << " " << i.second << "\n";
    }

    fin.close();
    fout.close();
    return 0;
}