Pagini recente » Cod sursa (job #2971849) | Cod sursa (job #26631) | Cod sursa (job #1638351) | Cod sursa (job #1399809) | Cod sursa (job #2056038)
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define NMAX 100010
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int N,M,i,x,y;
vector<int> v[NMAX];
int u,st[5*NMAX];
int main()
{
f>>N>>M;
for(i = 1 ; i <= M ; ++i) f>>x>>y , v[x].push_back(y) , v[y].push_back(x);
for(i = 1 ; i <= N ; ++i)
if( v[i].size()%2 )
{
g << -1 ;
return 0;
}
st[++u] = 1;
while(u)
{
x=st[u];
while( !v[x].empty() )
{
y = v[x].back();
v[x].pop_back();
v[y].erase( find(v[y].begin(),v[y].end(),x) );
st[++u] = y;
x = y;
}
g<<st[u--]<<' ';
}
return 0;
}
/*
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define NMAX 100010
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int N,M,i,x,y;
vector<int> v[NMAX];
stack<int>st;
int main()
{
f>>N>>M;
for(i = 1 ; i <= M ; ++i) f>>x>>y , v[x].push_back(y) , v[y].push_back(x);
for(i = 1 ; i <= N ; ++i)
if( v[i].size()%2 )
{
g << -1 ;
return 0;
}
st.push(1);
while( !st.empty() )
{
x=st.top();
while( !v[x].empty() )
{
y = v[x].back();
v[x].pop_back();
v[y].erase( find(v[y].begin(),v[y].end(),x) );
st.push(y);
x=y;
}
g<<st.top()<<' ';
st.pop();
}
return 0;
}
*/