Cod sursa(job #1491897)

Utilizator BaweeLazar Vlad Bawee Data 26 septembrie 2015 13:33:33
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <cstdio>

using namespace std;

int n,m;
int a[10001][10001];
int t[10001],viz[10001],g[10001];

void citire()
{
    int x,y;
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        cin >> x >> y;
        g[x]++;
        g[y]++;
        if(x == y) g[x]--;
        a[x][y] = a[y][x] = 1;
    }
}

void euler(int nc)
{
    int i ;
    cout << nc << " ";
    for(i = 1; i <= n; i++)
        if(a[nc][i])
            if(i != t[nc] && nc != t[i])
            {
                a[nc][i] = a[i][nc] = 0;
                euler(i);
            }
    for(i = 1; i <= n; i++)
        if(a[nc][i])
        {
            a[nc][i] = a[i][nc] = 0;
            euler(i);
        }
}

bool dfConex(int nc)
{
    viz[nc] = 1;
    for(int j = 1; j <= n; j++)
        if(!viz[j] && a[nc][j])
        {
            t[j] = nc;
            dfConex(j);
        }

    for(int i = 1; i <= n; i++)
        if(!viz[i])
            return false;
    return true;
}

bool cec()
{
    for(int i = 1; i <= n; i++)
        if(g[i] % 2)
            return false;
    return true;
}
int main()
{
    citire();
    if(cec && dfConex(1))
        euler(1);
    return 0;
}