Pagini recente » Cod sursa (job #2402368) | Cod sursa (job #1117217) | Cod sursa (job #1882357) | Cod sursa (job #889540) | Cod sursa (job #2681153)
// TemaExcelenta.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, i, j, x, y;
vector<pair<int, int> > l[100001];
int g[100001];
bitset<500001> f;
int st[500001], k;
int nod, vecin, sol[500001];
int dfs(int nod) {
for (int i = 0; i < l[nod].size(); i++)
if (f[l[nod][i].first] == 0) {
f[l[nod][i].first] = 1;
dfs(l[nod][i].first);
}
}
int main() {
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> x >> y;
g[x]++;
g[y]++;
l[x].push_back(make_pair(y, i));
l[y].push_back(make_pair(x, i));
}
dfs(1);
for (i = 1; i <= n; i++)
if (!f[i] || g[i] % 2 == 1) {
fout << "-1";
return 0;
}
f.reset();
st[++k] = 1;
while (k) {
nod = st[k];
if (g[nod] == 0) {
if (k != 1)
fout << nod << " ";
k--;
continue;
}
while (f[l[nod].back().second] == 1)
l[nod].pop_back();
vecin = l[nod].back().first;
f[l[nod].back().second] = 1;
l[nod].pop_back();
g[nod]--;
g[vecin]--;
k++;
st[k] = vecin;
}
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file