Cod sursa(job #2260754)

Utilizator Ioana_AndreeaCristescu Ioana Ioana_Andreea Data 15 octombrie 2018 15:35:29
Problema Mesaj4 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int NMax = 100000;
int N, M, k;
vector <int> G[NMax + 5];
bool Use[NMax + 5];
struct
{
    int x, y;
}Sol[NMax + 5];

void Read()
{
    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);
    }
}

void DFS(int Nod)
{
    Use[Nod] = 1;
    for (int i = 0; i<(int)G[Nod].size(); i++)
    {
        int Vecin = G[Nod][i];
        if (Use[Vecin] == 0)
        {
            DFS(Vecin);
            Sol[++k].x = Vecin;
            Sol[k].y = Nod;
        }
    }
}

int main()
{
    Read();
    DFS(1);
    for (int i=1; i<=N; i++)
        if (Use[i] == 0)
    {
        fout<<"-1";
        return 0;
    }
    int Timp = 2*(N-1);
    fout<<Timp<<"\n";
    for (int i=1; i<=k; i++)
        fout<<Sol[i].x<<" "<<Sol[i].y<<"\n";
    for (int i = k; i>=1; i--)
        fout<<Sol[i].y<<" "<<Sol[i].x<<"\n";
    return 0;
}