Cod sursa(job #1888369)

Utilizator FloareaCucura Floarea Ludovica Floarea Data 22 februarie 2017 08:17:31
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda becreative20 Marime 1 kb
#include <fstream>
#include <bitset>
#include <vector>
#define x first
#define y second

using namespace std;

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

int n, m, sol;
pair<int, int> S[100010];
vector<int>A[100010];
bitset<100010>viz;

void DFS(int nod)
{
    viz[nod] = 1;
    for(int i = 0; i < A[nod].size(); i ++){
      if(viz[ A[nod][i] ] == 0){
           DFS( A[nod][i] );
            sol++;
            S[sol].x = nod;
            S[sol].y = A[nod][i];
        }
    }
}

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

    for(int i=1;i<=m;i++)
    {
        int a, b;
        fin >> a >> b;
        A[a].push_back(b);
        A[b].push_back(a);
    }

    DFS(1);

    if(sol != n- 1)
    {
        fout << -1;
        return 0;
    }

    fout <<2*sol<<'\n';
    for(int i = 1; i <= sol; i ++){
       fout << S[i].y << " " << S[i].x << '\n';
    }
    for(int i = sol; i >= 1; i --){
       fout<<S[i].x << " " << S[i].y << '\n';
    }


    return 0;
}