Pagini recente » Borderou de evaluare (job #1567603) | Cod sursa (job #2272224) | Cod sursa (job #1424256) | Cod sursa (job #1913317) | Cod sursa (job #3223747)
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <vector>
const int32_t MAX_N = 100000;
const int32_t MAX_M = 500000;
std::vector<int32_t> adj[MAX_N];
int32_t stack[MAX_M + 1], top = 0;
int32_t res[MAX_M + 1], resLen = 0;
int main() {
std::ifstream fin("ciclueuler.in");
std::ofstream fout("ciclueuler.out");
int32_t n, m;
fin >> n >> m;
for(int32_t i = 0; i != m; ++i) {
int32_t x, y;
fin >> x >> y;
--x; --y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int32_t i = 0; i != n; ++i) {
if(adj[i].size() & 1) {
fout << "-1\n";
fin.close();
fout.close();
return 0;
}
}
stack[top++] = 0;
while(top) {
int32_t node = stack[top - 1];
if(!adj[node].empty()) {
int32_t next = adj[node].back();
adj[node].pop_back();
for(int32_t i = 0; i != adj[next].size(); ++i) {
if(adj[next][i] == node) {
adj[next].erase(adj[next].begin() + i);
break;
}
}
stack[top++] = next;
} else {
res[resLen++] = stack[top - 1];
--top;
}
}
for(int32_t i = 0; i != resLen - 1; ++i)
fout << (res[i] + 1) << ' ';
fin.close();
fout.close();
return 0;
}