Cod sursa(job #2789360)

Utilizator mihneadv@yahoo.comDavid Mihnea Stefan [email protected] Data 27 octombrie 2021 14:25:14
Problema Mesaj4 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#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<pair<int,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, v[start][i]});
            DFS(v[start][i]);
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL); fout.tie(NULL);
    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() == X - 1)
    {
        fout << 2 * X - 2 << '\n';
        for(int i = X - 2; i >= 0; i--)
        {
            fout << pereche[i].second + 1 << ' ' << pereche[i].first + 1 << '\n';
        }
        for(int i = 0; i < X - 1; i++)
        {
            fout << pereche[i].first + 1 <<  " " << pereche[i].second + 1 << '\n';
        }
    }
    else
    {
        fout << -1;
    }
    return false;
}