Cod sursa(job #2681153)

Utilizator sabinandreiBocan Sabin Andrei sabinandrei Data 5 decembrie 2020 02:44:23
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.07 kb
// 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