Cod sursa(job #1435171)

Utilizator batistaUPB-Oprea-Cosmin-Dumitru batista Data 12 mai 2015 12:53:07
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<fstream>
#include<algorithm>
#include<list>
#include<stack>
#define N 100010
using namespace std;
ofstream g("ciclueuler.out");
list<int> v[N];
list<int>::iterator it;
stack<int> st;
int n, m, i, x, y, ok;
void euler(int nd)
{
    int vec;
    st.push(nd);
    while(!st.empty())
    {
        nd = st.top();
        if(v[nd].size()==0)
        {
            g << nd << " ";
            st.pop();
            continue;
        }
        vec = v[nd].front();
        v[nd].pop_front();
        st.push(vec);
        v[vec].remove(nd);
        /*for(it = v[vec].begin(); it != v[vec].end(); it++)
            if(*it == nd)
            {
                v[vec].erase(it);
                break;
            }*/
    }
}
int main()
{
    ifstream f("ciclueuler.in");
    f >> n >> m;
    for(i = 1; i <= m; i++)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    ok=1;
    for(i = 1; i <= n; i++)
        if(v[i].size() & 1)
        {
            ok=0;
            break;
        }

    if(!ok) g << "-1\n";
    else
        euler(1);
    f.close();
    g.close();
    return 0;
}