Cod sursa(job #2527419)

Utilizator PetrescuAlexandru Petrescu Petrescu Data 20 ianuarie 2020 12:22:48
Problema Ciclu Eulerian Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <fstream>
#include <vector>
#include <utility>
#include <iostream>
#define MAX 100000

using namespace std;

vector< pair<int, int> >graph[MAX + 1];
vector<int> listNodes;
int stiva[MAX * 5 + 1];
bool vizitat[5 * MAX + 1];
int edgeCounter = 0, edgeNr = 0;

void DFS(int nod, int depth)
{

    cout << depth << " ";

}

int main()
{
    int n, m, a, b;

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

    fin >> n >> m;

    for(int i = 0; i < m; i++)
    {
        fin >> a >> b;

        graph[a].push_back({b, edgeCounter});
        graph[b].push_back({a, edgeCounter});

        edgeCounter++;
    }

    int height = 1;

    stiva[height] = 1;

    while(height)
    {
        int nod = stiva[height];
        //cout<<nod<<" ";

        int i;
        for(i = 0; i < graph[nod].size(); i++)
        {
            if(!vizitat[graph[nod][i].second])
            {
                height++;
                stiva[height] = graph[nod][i].first;

                vizitat[graph[nod][i].second] = 1;

                i = graph[nod].size();
            }
        }

        if(i == graph[nod].size())
        {
            listNodes.push_back(nod);

            height--;
        }
    }
    listNodes.pop_back();

    if(listNodes.size() == edgeCounter)for(auto i : listNodes)fout << i << " ";
    else fout << -1;

    fin.close();
    fout.close();

    return 0;
}