Pagini recente » Cod sursa (job #243931) | Clasament make_lasm_great_again | Cod sursa (job #2778664) | Cod sursa (job #745840) | Cod sursa (job #2846679)
#include <bits/stdc++.h>
#define Mmax 500005
#define Nmax 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct three{
int first,second;
bool third;
};
three M[Mmax];
vector<int>G[Nmax],sol;
int n,m;
bool Verif()
{
for(int i = 1; i <= n; i++)
if(G[i].size() & 1)
return 0;
return 1;
}
void Euler(int nod)
{
stack<int> S;
S.push(1);
while(!S.empty())
{
int nod = S.top();
if(!G[nod].size())
{
sol.push_back(nod);
S.pop();
}
else
{
int muchie = G[nod].back();
G[nod].pop_back();
if(!M[muchie].third)
{
M[muchie].third = true;
if(M[muchie].first == nod)
S.push(M[muchie].second);
else
S.push(M[muchie].first);
}
}
}
}
int main()
{
f>>n>>m;
for(int i = 1; i <= m; i++)
{
int x,y;
f>>x>>y;
M[i]={x,y,false};
G[x].push_back(i);
G[y].push_back(i);
}
if(!Verif())
{
g<<"-1\n";
return 0;
}
Euler(1);
g<<sol.size()-1<<"\n";
for(vector<int>::iterator it = sol.begin(); it < sol.end()-1; it++)
g<<*it<<" ";
return 0;
}