Cod sursa(job #2527446)

Utilizator PetrescuAlexandru Petrescu Petrescu Data 20 ianuarie 2020 13:02:35
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 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++;
    }

    bool semafor = true;
    for(int i = 1; i <= n && semafor == true; i++)if(graph[i].size() % 2)semafor = false;

    if(semafor)
    {
        int height = 1;

        stiva[height] = 1;

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

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

                    vizitat[graph[nod][i].second] = 1;
                    graph[nod].pop_back();

                    i = -1;
                }
            }

            if(i == -1)
            {
                fout << nod << " ";

                height--;
            }
        }
    }else fout << -1;

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

    return 0;
}