Cod sursa(job #2171182)

Utilizator CozmaCatalinCozma Catalin CozmaCatalin Data 15 martie 2018 11:29:27
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <bits/stdc++.h>

std::ifstream in("ciclueuler.in");
std::ofstream out("ciclueuler.out");

using namespace std;

const int MAX = 500005;
bitset < MAX > beenThere;
vector < pair < int , int > > myVector[MAX];
stack < int > myStack;
vector < int > Answer;
int Grad[MAX];

int N,countt,M;

inline void scanDATA()
{
    in >> N >> M;
    int x,y;
   for ( int i = 1; i <= M ; ++i)
    {
        in >> x >> y;
        countt++;
        myVector[x].push_back(make_pair(y,countt));
        myVector[y].push_back(make_pair(x,countt));
        Grad[x]++;
        Grad[y]++;
    }
}
inline void EULER()
{
    myStack.push(1);

    while(!myStack.empty())
    {
        int currentNode = myStack.top();
        if(myVector[currentNode].size() > 0)
        {
            int nod = myVector[currentNode].back().first;
            int edge = myVector[currentNode].back().second;
            myVector[currentNode].pop_back();
            if(beenThere[edge] == false)
            {
                beenThere[edge] = true;
                myStack.push(nod);
            }
        }
        else
        {
            myStack.pop();
            Answer.push_back(currentNode);
        }
    }
 // cout << Answer.size() <<"\n";
    for ( auto x : Answer)
        out << x <<" ";
}

int main()
{
    scanDATA();
    for ( int i = 1; i <= N ; ++i)
        if(Grad[i] %2 !=0 || Grad[i] == 0)
    {
        out << -1;
        return 0;
    }
    EULER();

}