Cod sursa(job #1249942)

Utilizator OnimushaLordTiberiu Copaciu OnimushaLord Data 27 octombrie 2014 17:54:17
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
        #include <cstdio>
        #include <vector>
        #include <list>
        using namespace std;
        vector <int> g[100001];
        list <int> q;
        void euler (int x)
        {
            vector <int> :: iterator it;
            q.push_front(x);
            while (!q.empty())
            {
                x=q.front();
                if (g[x].empty())
                {
                    q.pop_front();
                    if (!q.empty()) printf("%d ",x);
                }
                else
                {
                    int y=g[x].back();
                    q.push_front(y);
                    g[x].pop_back();
                    for (it=g[y].begin(); it!=g[y].end(); it++)
                    {
                        if (*it==x)
                        {
                            g[y].erase(it);
                            break;
                        }
                    }
                }
            }
        }
        int main()
        {
            int m, n, i, x, y;
            freopen("ciclueuler.in","r",stdin);
            freopen("ciclueuler.out","w",stdout);
            scanf("%d%d",&n,&m);
            for (i=1; i<=m; i++)
            {
                scanf("%d%d",&x,&y);
                g[x].push_back(y);
                g[y].push_back(x);
            }
            euler(1);
            fclose(stdin);
            fclose(stdout);
            return 0;
        }