Mai intai trebuie sa te autentifici.
Cod sursa(job #1277389)
Utilizator | Data | 27 noiembrie 2014 16:49:57 | |
---|---|---|---|
Problema | Ciclu Eulerian | Scor | 50 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.17 kb |
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int N, M, x, y;
vector < int > G[100005], sol;
stack < int > st;
void df(int nod)
{
int nou;
if(G[nod].size())
{
nou=G[nod][0];
G[nod].erase(G[nod].begin()); st.push(nou);
G[nou].erase( find(G[nou].begin(), G[nou].end(), nod) );
df(nou);
}
}
void ciclu(int x)
{
st.push(x);
while (!st.empty())
{
int nod=st.top();
/*if (G[nod].size())
{
int last=G[nod].back();
G[nod].pop_back(); st.push(last);
G[last].erase( find(G[last].begin(), G[last].end(), nod) );
}*/
// else
df(nod);
sol.push_back(nod), st.pop();
}
}
int main()
{
f>>N>>M;
for (int i=1; i<=M; ++i)
f>>x>>y, G[x].push_back(y), G[y].push_back(x);
for (int i=1; i<=N; ++i)
if (G[i].size()&1) { g<<-1<<'\n'; return 0; }
ciclu(1);
for (int i=0; i<(int)sol.size()-1; ++i)
g<<sol[i]<<' ';
g<<"alabalaportocala";
return 0;
}