Pagini recente » Cod sursa (job #429071) | Cod sursa (job #60960) | Cod sursa (job #1148072) | Cod sursa (job #2594680) | Cod sursa (job #929401)
Cod sursa(job #929401)
#include <fstream>
#include <vector>
#include <deque>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, m2;
int gr[100001];
bool muchii[500001];
struct vecin
{
int nod;
int muchie;
};
bool v[100001];
vector <vecin> lv[100001];
vector <int> st;
deque <int> c;
void BFS(int nod)
{
int i;
c.push_back(nod);
v[nod] = true;
while(!c.empty())
{
nod = c.front();
c.pop_front();
for( i = 0; i < lv[nod].size(); i++)
if(!v[lv[nod][i].nod])
{
v[lv[nod][i].nod]=true;
c.push_back(lv[nod][i].nod);
}
}
}
bool check()
{
int i;
for( i = 1; i <= n; i++)
if( gr[i] % 2 ) return 0;
BFS(1);
for( i = 1; i <= n; i++)
if( v[i] == false ) return 0;
return 1;
}
void euler(int nod)
{
int aux,i;
i=lv[nod].size();
while(i && muchii[lv[nod][i-1].muchie] == true)
{
i--;
lv[nod].pop_back();
}
if(i && m2 < m-1)
{
muchii[lv[nod][i-1].muchie] = true;
aux=lv[nod][i-1].nod;
m2++;
fout << aux<<" ";
lv[nod].pop_back();
st.push_back(aux);
euler(aux);
}
}
void rezolvare()
{
while(!st.empty())
{
int x = st.back();
st.pop_back();
euler(x);
}
}
int main()
{
fin >> n >> m;
int i;
for( i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
gr[x]++;
gr[y]++;
vecin aux;
aux.nod=x;
aux.muchie=i;
lv[y].push_back(aux);
aux.nod=y;
lv[x].push_back(aux);
}
if(check())
{
fout<< 1<<" ";
euler(1);
}
else fout << -1;
return 0;
}