Pagini recente » Rating Bogdan-Constantin Enache (L3ul) | Diferente pentru agm-2018/runda1 intre reviziile 16 si 20 | Cod sursa (job #1298044) | Cod sursa (job #652807) | Cod sursa (job #2247709)
#include <algorithm>
#include <fstream>
#include <list>
#include <stack>
#include <vector>
using namespace std;
using Graph = vector<list<int>>;
void RemoveEdge(Graph &g, int node1, int node2)
{
g[node1].erase(find(g[node1].begin(), g[node1].end(), node2));
g[node2].erase(find(g[node2].begin(), g[node2].end(), node1));
}
int main()
{
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int nodes, edges;
fin >> nodes >> edges;
vector<list<int>> graph(nodes);
vector<int> degrees(nodes, 0);
for (int i = 0; i < edges; i += 1) {
int a, b;
fin >> a >> b;
graph[a - 1].push_front(b - 1);
graph[b - 1].push_front(a - 1);
degrees[a - 1] += 1;
degrees[b - 1] += 1;
}
for (const auto ° : degrees) {
if (deg % 2 == 1) {
fout << "-1\n";
return 0;
}
}
stack<int> st;
st.push(0);
while (!st.empty()) {
auto node = st.top();
if (graph[node].empty()) {
st.pop();
if (!st.empty()) {
fout << node + 1 << " ";
}
} else {
auto next = graph[node].front();
st.push(next);
RemoveEdge(graph, node, next);
}
}
fout << "\n";
return 0;
}