Cod sursa(job #2467454)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 4 octombrie 2019 14:01:23
Problema Mesaj4 Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
/// Mesaj4
/// Toate mesajele stranse in nodul de gard maxim cu n-1 pasi
/// Distribuirea lor la restul de noduri prin n-1 pasi
#include <vector>
#include <fstream>
#include <utility>
using namespace std;
ifstream in("mesaj4.in");
ofstream out("mesaj4.out");
#define NMAX 100000
vector <int> G[NMAX+5];
int N, M, Use[NMAX+5], Grad[NMAX+5], K;
pair <int,int> V[NMAX+5];

void Read () {
    in >> N >> M;

    for (int i = 0, x, y; i < M; i++) {
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void DFS1 (int Node) {
    Use[Node] = 1;

    for (int i = 0; i < (int)G[Node].size(); i++) {
        int Vecin = G[Node][i];
        if (!Use[Vecin]) {
            V[++K] = {Node, Vecin};///inlocuieste DFS2
            DFS1(Vecin);
            out << Vecin << " " << Node << '\n';
        }
    }
}

int main () {
    Read();

    for(int i = 1; i <= N; i++)
        if(G[i].empty()) {
            out << "-1\n";
            return 0;
        }
    out << 2 * N - 2 << '\n';
    DFS1(1);

    for(int i = 1; i < N; i++)
        out << V[i].first << " " << V[i].second << '\n';

    return 0;
}