Pagini recente » Cod sursa (job #1397860) | Cod sursa (job #2963777) | Cod sursa (job #2716717) | Cod sursa (job #562553) | Cod sursa (job #2092099)
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
#define f first
#define s second
using namespace std;
ifstream F("ciclueuler.in");
ofstream G("ciclueuler.out");
int n, m, x, y, grd[100005], L, l[100005], z;
vector<pair<int, int> > a[100005];
bitset<500005> v;
stack<int> st;
int main()
{
F >> n >> m;
for( int i = 1; i <= m; ++ i )
{
F >> x >> y;
a[x].push_back({y, i});
a[y].push_back({x, i});
grd[x]++; grd[y]++;
}
for( int i = 1; i <= n; ++ i )
if( grd[i] % 2 || !grd[i] )
{
G << -1;
return 0;
}
st.push(1);
while(!st.empty())
{
x = st.top();
L = a[x].size();
while(l[x] < L)
{
y = a[x][l[x]].f; z = a[x][l[x]].s;
if(!v[z])
{
v[z] = 1;
st.push(y);
break;
}
l[x]++;
}
if(l[x] == L)
{
G << x << " ";
st.pop();
}
}
return 0;
}