Pagini recente » Cod sursa (job #361414) | Cod sursa (job #1525964) | Cod sursa (job #1239684) | Cod sursa (job #2136844) | Cod sursa (job #3161366)
#include <fstream>
#include <vector>
#include <stack>
#define pb push_back
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
const int N = 1e5 + 5, M = 5e5 + 5;
struct muchie {
int nxt, cnt;
};
int n, m, grad[N];
bool luat[M];
vector<int> ciclu;
vector<muchie> g[N];
stack<int> stk;
int main()
{
in >> n >> m;
for(int i=1; i<=m; i++) {
int x, y;
in >> x >> y;
grad[x]++;
if(x != y)
grad[y]++;
g[x].pb({y, i});
g[y].pb({x, i});
}
for(int i=1; i<=n; i++)
if(g[i].size() % 2) {
out << -1;
return 0;
}
stk.push(1);
while(!stk.empty()) {
int nod = stk.top();
if(grad[nod]) {
for(auto mc : g[nod]) {
if(!luat[mc.cnt]) {
stk.push(mc.nxt);
grad[nod]--;
if(mc.nxt != nod)
grad[mc.nxt]--;
luat[mc.cnt] = 1;
break;
}
}
}
else {
stk.pop();
ciclu.pb(nod);
}
}
for(int i=0; i<ciclu.size()-1; i++)
out << ciclu[i] << ' ';
return 0;
}