Cod sursa(job #2789332)

Utilizator mihneadv@yahoo.comDavid Mihnea Stefan [email protected] Data 27 octombrie 2021 13:49:51
Problema Mesaj4 Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

vector<vector<int>>v;
bitset <100000>ap;
vector<int>pereche;

void DFS(int start)
{
    ap[start] = true;
    for (int i = 0; i < v[start].size(); i++)
    {
        if(!ap[v[start][i]])
        {
            pereche.push_back(start + 1);
            pereche.push_back(v[start][i] + 1);
            DFS(v[start][i]);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    int X, U;
    fin >> X >> U;
    v.resize(X);
    for(int i = 0; i < U ; i++)
    {
        int V1, V2;
        fin >> V1 >> V2;
        V1--, V2--;
        v[V1].push_back(V2);
        v[V2].push_back(V1);
    }
    DFS(false);
    if(pereche.size() == 2 * X - 2)
    {
        fout << 2 * X - 2 << '\n';
        for(int i = 2 * X - 3; i >= 0; i -= 2)
        {
            fout << pereche[i] << ' ' << pereche[i - 1] << '\n';
        }
        for(int i = 0; i < 2 * X - 2; i += 2)
        {
            fout << pereche[i] << ' ' << pereche[i + 1] << '\n';
        }
    }
    else
    {
        fout << -1;
    }
    return false;
}