Cod sursa(job #2215181)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 21 iunie 2018 11:27:45
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <queue>

using namespace std;

ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");

int n, m, v[1001][1001], x, y;
vector <int> st;

void dfs (int nod)
{
    int Max = 0, poz = 0;
    for (int i = 1; i <= n; i++)
    {
        if (v[nod][i] == 1)
        {
            if (Max < v[i][0])
            {
                Max = v[i][0];
                poz = i;
            }
        }
    }

    if (poz != 0) {
    v[nod][poz] = v[poz][nod] = 0;
    v[nod][0]--;
    v[poz][0]--;
    st.push_back(poz);
    dfs(poz);
    }
}
int main()
{
    f >> n >> m;

    for (int i = 1; i <= m; i++)
    {
         f >> x >> y;
         v[x][y] = v[y][x] = 1;
         v[x][0] ++;
         v[y][0] ++;
    }

    dfs(1);

    for (int i = 0; i < st.size(); i++)
    {
        g << st[i] << " ";
    }
    return 0;
}