Pagini recente » Cod sursa (job #1845475) | Cod sursa (job #2462021) | Cod sursa (job #1854333) | Cod sursa (job #686474) | Cod sursa (job #2712097)
#include <fstream>
#include <vector>
#include <stack>
#include <unordered_set>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax = 100000;
const int mmax = 500000;
struct str {
int x, y;
};
vector <str> g[nmax+1];
bool un[nmax+1];
void dfs( int x ) {
un[x] = 1;
for ( int i = 0; i < int(g[x].size()); ++i ) {
int xn = g[x][i].x;
if ( un[xn] == 0 ) {
dfs(xn);
}
}
}
stack <int> s;
bool um[mmax+1];
bool g_empty( int x ) {
while ( g[x].size() > 0 && um[g[x][g[x].size()-1].y] != 0 ) {
g[x].pop_back();
}
return g[x].empty();
}
int main( ) {
int n, m;
fin >> n >> m;
for ( int i = 1; i <= m; ++ i ) {
int x, y;
fin >> x >> y;
str aux;
aux.x = y;
aux.y = i;
g[x].push_back(aux);
aux.x = x;
g[y].push_back(aux);
}
int ok = 1;
dfs(1);
for ( int i = 1; i <= n; ++ i ) {
if ( g[i].size()%2 == 1 || un[i] == 0 ) {
ok = 0;
}
}
if ( ok == 1 ) {
s.push(1);
for ( int i = 1; i <= m; ++ i ) {
int x = s.top();
while ( g_empty(x) == 0 ) {
str y = g[x][g[x].size()-1];
g[x].pop_back();
um[y.y] = 1;
x = y.x;
s.push(x);
}
fout << x << " ";
s.pop();
}
} else {
fout << "-1\n";
}
return 0;
}