Mai intai trebuie sa te autentifici.
Cod sursa(job #2864539)
Utilizator | Data | 7 martie 2022 22:34:55 | |
---|---|---|---|
Problema | Ciclu Eulerian | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.28 kb |
#include <bits/stdc++.h>
#define Dmax 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct muchie
{
int first,second;
bool third;
};
int n,m;
vector<int>G[Dmax];
vector<muchie>M;
int main()
{
f>>n>>m;
for(int i = 1; i <= m; i++)
{
int x,y;
f>>x>>y;
M.push_back({x,y,false});
G[x].push_back(i);
G[y].push_back(i);
}
for(int i = 1; i <= n; i++)
if(G[i].size() & 1)
{
g<<"-1\n";
return 0;
}
stack<int>S;
vector<int>sol;
S.push(1);
while(!S.empty())
{
int nod = S.top();
if(G[nod].empty())
{
sol.push_back(nod);
S.pop();
}
else
{
int muchie = G[nod].back();
G[nod].pop_back();
muchie--;
if(!M[muchie].third)
{
M[muchie].third = true;
if(M[muchie].first == nod)
S.push(M[muchie].second);
else
S.push(M[muchie].first);
}
}
}
for(vector<int>::iterator it = sol.begin(); it < sol.end() - 1; it++)
g<<*it<<" ";
return 0;
}