Cod sursa(job #2825653)

Utilizator calinstefan025Avramoniu Calin calinstefan025 Data 4 ianuarie 2022 22:56:59
Problema Mesaj4 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;

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

int n , m , x , y ;
vector<int> g[100001] ;
vector<pair<int , int>> rez ;
int viz[100001] , cc ;


void dfs(int nod) {
    viz[nod] = 1;
    for(auto val : g[nod]) {
        if(!viz[val]) {
            rez.push_back(make_pair(val, nod));
            dfs(val) ;
        }
    }
}

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

    dfs(1) ;
    for(int i = 1 ; i<=n ; i++) {
        if(!viz[i]) {
            fout << "-1" << endl ;
            return 0 ;
        }
    }

    fout << 2 * (n-1) << '\n' ;
    for (int i = rez.size() - 1; i >= 0; i--)
        fout << rez[i].first << ' ' << rez[i].second << '\n';
    for (int i = 0; i < rez.size(); i++)
        fout << rez[i].second << ' ' << rez[i].first << '\n';

    return 0;
}