Pagini recente » Cod sursa (job #2474698) | Cod sursa (job #2191137) | Autor necunoscut | Cod sursa (job #1347753) | Cod sursa (job #969193)
Cod sursa(job #969193)
/*
<b> Un graf este EULERIAN daca este CONEX si gradul fiecarul nod este PAR </b>
*/
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <ctime>
#include <utility>
#include <stack>
using namespace std;
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
const int MAXN = 100005;
int n, m;
vector <int> Graph[MAXN];
stack <int> Stack;
inline void ReadData()
{
cin >> n >> m;
for(int i = 1 ; i <= m ; ++ i)
{
int x, y;
cin >> x >> y;
Graph[x].push_back(y);
Graph[y].push_back(x);
}
}
inline bool CheckEulerProperty()
{
for(int i = 1 ; i <= n ; ++ i)
if(!Graph[i].size() || Graph[i].size() & 1)
return false;
return true;
}
inline void DepthFirstSearch()
{
Stack.push(1);
while( !Stack.empty() )
{
int x = Stack.top();
if(Graph[x].size())
{
int y = Graph[x].back();
Stack.push(y);
Graph[x].pop_back();
Graph[y].erase(find(Graph[y].begin(), Graph[y].end(), x));
}
else /// return from recursion
{
Stack.pop();
if(!Stack.empty())
cout << x << " ";
}
}
}
int main()
{
ReadData();
if( !CheckEulerProperty() )
{
cout << "-1\n";
return 0;
}
DepthFirstSearch();
return 0;
}