Pagini recente » Cod sursa (job #2107739) | Cod sursa (job #716364) | Cod sursa (job #2742362) | Cod sursa (job #2246747) | Cod sursa (job #2859648)
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define NMAX 100000
#define NRM 500000
typedef pair<int, int> PII;
int n, m;
bool sel[NRM + 1]; //ce muchii vizitez;
vector<PII> G[NRM + 1];
vector<int> ans;
static inline bool IsEuler() {
int ok = 1;
for(int i = 1; i <= n; i++)
if(G[i].size() % 2 == 1)
ok = 0;
return ok;
}
int main() {
fin >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
G[x].push_back({y, i}); //unde merg si a cata muchie e;
G[y].push_back({x, i});
}
if(!IsEuler())
fout << -1;
else {
stack<int> ST;
ST.push(1);
while(!ST.empty()) {
int nod = ST.top();
if(G[nod].size()) { //daca mai am vecini;
int x = G[nod].back().first;
int muchie = G[nod].back().second;
G[nod].pop_back();
if(!sel[muchie]) {
sel[muchie] = 1;
ST.push(x);
}
}else {
ST.pop();
ans.push_back(nod);
}
}
ans.pop_back();
for(auto e : ans)
fout << e << " ";
}
return 0;
}