Pagini recente » Istoria paginii runda/pregatire_onig | Cod sursa (job #2032968)
#include <fstream>
#include <vector>
#include <cstdio>
#include <stack>
#include <algorithm>
#define VAL 100005
using namespace std;
int N, M, i, j;
int A, B, nod, nr;
int X[VAL];
stack <int> S;
vector <int> G[VAL];
vector <int> :: iterator it;
bool ciclu=true;
char buff[VAL];
int poz=0;
int Read_INT()
{
int nr=0;
while (buff[poz]<'0' || buff[poz]>'9')
if (++poz==VAL)
fread(buff, 1, VAL, stdin), poz=0;
while ('0'<=buff[poz] && buff[poz]<='9')
{
nr=nr*10+buff[poz]-'0';
if (++poz==VAL)
fread(buff, 1, VAL, stdin), poz=0;
}
return nr;
}
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
N=Read_INT();
M=Read_INT();
for (i=1; i<=M; i++)
{
A=Read_INT();
B=Read_INT();
X[A]++;
X[B]++;
G[A].push_back(B);
G[B].push_back(A);
}
for (i=1; i<=N; i++)
{
if (X[i] % 2==1)
{
ciclu=false;
break;
}
}
if (ciclu==false)
printf("-1\n");
else
{
nod=1;
while (1)
{
if (G[nod].empty()==true && S.empty()==true)
break;
if (G[nod].empty()==true)
{
printf("%d ", nod);
nod=S.top();
S.pop();
}
else
{
S.push(nod);
nr=G[nod].back();
G[nod].pop_back();
G[nr].erase(find(G[nr].begin(), G[nr].end(), nod));
nod=nr;
}
}
}
return 0;
}