Cod sursa(job #2789299)

Utilizator mihneadv@yahoo.comDavid Mihnea Stefan [email protected] Data 27 octombrie 2021 12:51:53
Problema Mesaj4 Scor 85
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;
pair<int,int>pereche[100000];
int ind = false;

inline void DFS(int start)
{
    ap[start] = true;
    for (int i = 0; i < v[start].size(); i++)
    {
        if(!ap[v[start][i]])
        {
            pereche[ind++] = {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(ind == 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;
}