Cod sursa(job #2343184)

Utilizator BotzkiBotzki Botzki Data 13 februarie 2019 19:04:29
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <string>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX=100000;
bitset<NMAX+5>viz;
vector <int>G[NMAX+5];
int st[500005], top;
string s;
void dfs(int nod)
{
    int i;
    viz[nod]=1;
    for(i=0;i<G[nod].size();i++)
    {
        if(viz[G[nod][i]]==0)
            dfs(G[nod][i]);
    }

}
void eulerian(int nod)
{
    int other;
    while(!G[nod].empty())
    {
        st[++top]=nod;
        other=G[nod].back();
        G[nod].pop_back();
        G[other].erase(find(G[other].begin(), G[other].end(), nod));
        nod=other;
    }
}
int main()
{
    int n, m, i, x, y, k, nr, j;
    fin>>n>>m;
    fin.get();
    for(i=1;i<=m;i++)
    {
        getline(fin, s);
        x=0;
        y=0;
        for(j=0;j<s.size();j++)
        {
            nr=0;
            k=j;
            while(s[k]>='0' and s[k]<='9')
            {
                nr=nr*10+(s[k]-'0');
                k++;
            }
            if(x==0)
                x=nr;
            else
                y=nr;
            j=k;
        }
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(i=1;i<=n;i++)
    {
        if(G[i].size()%2!=0)
        {
            fout<<"-1"<<"\n";
            return 0;
        }
    }
    //dfs(1);
   /* for(i=1;i<=n;i++)
    {
        if(viz[i]==0)
        {
           fout<<"-1"<<"\n";
           return 0;
        }
    }*/    //am verificat daca graful este conex si daca gradele nodurilor sunt pare
    int nod=1;
    do{
      eulerian(nod);
      nod=st[top];
      fout<<nod<<" ";
      top--;
    }while(top>0);

    return 0;
}