Pagini recente » Cod sursa (job #613892) | Cod sursa (job #486412) | Cod sursa (job #2450195) | Cod sursa (job #1944040) | Cod sursa (job #2647387)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
class InParser {
private:
const int buff_sz = 16384;
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == buff_sz) {
sp = 0;
fread(buff, 1, buff_sz, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[buff_sz]();
sp = buff_sz-1;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (char &c) {
c = read_ch();
return *this;
}
};
InParser fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int N = 100005;
const int M = 500005;
// <destinatie, identificator muchie>
vector<pair<int, int>> g[N];
vector<int> ord;
bool viz[M];
int n, m;
void dfs(int nod)
{
stack<int> st;
st.push(nod);
while(!st.empty())
{
int nod = st.top();
st.pop();
// am consumat toate muchiile din nod
if(g[nod].size() == 0)
ord.push_back(nod);
else
{
int dest = g[nod].back().first;
int id = g[nod].back().second;
g[nod].pop_back();
st.push(nod);
if(!viz[id])
{
viz[id] = true;
st.push(dest);
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 0; i < m; i++)
{
int x, y;
fin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
// verificare
for(int i = 1; i <= n; i++)
if(g[i].size() % 2)
{
fout << -1;
return 0;
}
dfs(1);
ord.pop_back();
for(int x : ord)
fout << x << ' ';
return 0;
}