Pagini recente » Cod sursa (job #1358138) | Cod sursa (job #968811) | Cod sursa (job #135913) | Cod sursa (job #348113) | Cod sursa (job #742475)
Cod sursa(job #742475)
#include <vector>
#include <stack>
#include <algorithm>
#include <fstream>
using namespace std;
#define nmax 100010
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
vector<int> graph[nmax];
vector<int> solution;
int n,m,x,y;
void read_input()
{
in>>n>>m;
char s[15];
for(int i=0;i<m;i++)
{
in>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
bool Eulerian()
{
for(int i=1;i<=n;i++) if(graph[i].size()%2) return false;
return true;
}
void euler(int start)
{
stack <int> s;
s.push(start);
int node,nextNode;
while(!s.empty())
{
node=s.top();
while(!graph[node].empty())
{
nextNode= *graph[node].begin();
graph[nextNode].erase(find(graph[nextNode].begin(),graph[nextNode].end(),node));
graph[node].erase(graph[node].begin());
s.push(nextNode);
node=nextNode;
}
solution.push_back(s.top());
s.pop();
}
solution.pop_back();
for(vector<int>::iterator it=solution.begin();it!=solution.end();++it) out<<*it;
out<<"\n";
}
int main()
{
int i;
read_input();
if(Eulerian()==true) euler(1);
else out<<"-1\n";
in.close();
out.close();
return 0;
}