Pagini recente » Cod sursa (job #608386) | Cod sursa (job #316493) | Cod sursa (job #2990687) | Cod sursa (job #453110) | Cod sursa (job #2814953)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
//std::ifstream f("in.in");
//std::ofstream g("out.out");
class InParser
{
private:
FILE* fin;
char* buffer;
size_t SIZE = 4096;
int buffer_index;
char get_char()
{
++buffer_index;
if (buffer_index == SIZE)
{
size_t count = fread(buffer, 1, SIZE, fin);
if (count < SIZE)buffer[count] = 0;
buffer_index = 0;
}
return buffer[buffer_index];
}
public:
InParser(const char* name)
{
fin = fopen(name, "r");
buffer = new char[SIZE];
memset(buffer, 0, SIZE);
buffer_index = SIZE - 1;
}
~InParser()
{
fclose(fin);
delete[] buffer;
}
InParser& operator >>(uint32_t& x)
{
char c = get_char();
while (!isdigit(c))
c = get_char();
x = c - '0';
while (isdigit(c = get_char()))
x = x * 10 + c - '0';
return *this;
}
InParser& operator >>(int& x)
{
char c;
while (!isdigit(c = get_char()));
x = c - '0';
while (isdigit(c = get_char()))
x = x * 10 + c - '0';
return *this;
}
};
class OutParser
{
private:
FILE* fout;
char* buffer;
int buffer_index;
const int SIZE = 4096;
void print_char(char c)
{
if (buffer_index == SIZE)
{
fwrite(buffer, 1, SIZE, fout);
buffer_index = 0;
}
buffer[buffer_index++] = c;
}
public:
OutParser(const char* name)
{
fout = fopen(name, "w");
buffer_index = 0;
buffer = new char[SIZE];
memset(buffer, 0, SIZE);
}
~OutParser()
{
fwrite(buffer, 1, buffer_index, fout);
fclose(fout);
delete[] buffer;
}
OutParser& operator <<(uint32_t x)
{
if (x < 10)
print_char('0' + x);
else
{
*this << x / 10;
print_char('0' + x % 10);
}
return *this;
}
OutParser& operator <<(char c)
{
print_char(c);
return *this;
}
OutParser& operator <<(const char* c)
{
while (*c)
{
print_char(*c);
++c;
}
return *this;
}
};
int n, m;
InParser f("ciclueuler.in");
OutParser g("ciclueuler.out");
struct pair_hash {
std::size_t operator () (const std::pair<int, int>& p) const {
return (p.first * 1ULL * 42894574067ULL) ^ (p.second * 1ULL * 92746384501ULL);
}
};
typedef std::pair<int, int> iPair;
typedef unsigned long long ULL;
std::vector<std::vector<int> > la;
std::unordered_map<ULL, int> edges;
std::vector<int> cycle;
std::vector<int> deg;
//void dfs(int x) {
//
// struct stackInfo {
// int currentNode;
// int currentIndex;
// };
//
// std::stack<stackInfo> s;
//
// s.push({ 1,0 });
//
// while (!s.empty()) {
// auto top = s.top();
// s.pop();
//
// int n = top.currentNode;
// int i = top.currentIndex;
//
// if (i == la[n].size()) {
// cycle.push_back(n);
// continue;
// }
//
// s.push({ n, i + 1 });
//
// int vecin = la[n][i];
//
// int p1 = std::min(n, vecin);
// int p2 = std::max(n, vecin);
//
// if (edges[{p1, p2}]) {
// edges[{p1, p2}]--;
// s.push({ vecin, 0 });
// }
// }
//
//}
void dfs(int x) {
for (const int& i : la[x]) {
if (edges[(std::min(x,i) << 18) | std::max(x,i)]) {
edges[(std::min(x,i) << 18) | std::max(x,i)]--;
dfs(i);
}
}
cycle.push_back(x);
}
bool checkEuler() {
for (int i = 1; i <= n; ++i)
if (deg[i] & 1) return false;
std::queue<int> q;
q.push(1);
std::vector<bool>used(n + 1);
used[1] = 1;
while (!q.empty()) {
int t = q.front();
q.pop();
for(int i : la[t])
if (!used[i]) {
used[i] = 1;
q.push(i);
}
}
for (int i = 1; i <= n; ++i)
if (!used[i])return false;
return true;
}
int main() {
f >> n >> m;
la.resize(n + 1);
deg.resize(n + 1);
for (int i = 0; i < m; ++i) {
int x, y;
f >> x >> y;
deg[x]++;
deg[y]++;
if (x > y)
std::swap(x, y);
edges[ (x << 18) | y]++;
if (edges[(x << 18) | y] == 1) {
la[x].push_back(y);
if(y != x)
la[y].push_back(x);
}
}
if (!checkEuler()) {
g << "-1";
}
else {
dfs(1);
cycle.pop_back();
for (int i : cycle)
g << (uint32_t)i << ' ';
}
}