Cod sursa(job #1641180)

Utilizator serbanSlincu Serban serban Data 8 martie 2016 21:27:25
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <fstream>
#include <vector>
#include <stack>
#include <cstring>
#define oo 1111111

using namespace std;

ofstream g("ciclueuler.out");

bool vm[500005];
vector<int> L[100005];
stack<int> s;

int XX[5];
char str[1000];
void pt() {
    XX[0] = 0;
    int N = strlen(str) - 1, aux = 0;
    for(int i = 0; i <= N; i ++) {
        if(str[i] == ' ') XX[++ XX[0]] = aux, aux = 0;
        else aux += str[i] - '0';
    }
    XX[++ XX[0]] = aux;
}

int k = - 1, nr;
char out[3500000];

int main()
{
    freopen("ciclueuler.in", "r", stdin);
    int n, m, x, y; gets(str); pt();
    n = XX[1]; m = XX[2];
    for(int i = 0; i < m; i ++) {
        gets(str); pt();
        x = XX[1]; y = XX[2];
        L[x].push_back(y);
        L[y].push_back(x);
    }

    for(int i = 1; i <= n; i ++) {
        if(L[i].size() & 1) {
            g << "-1\n";
            return 0;
        }
    }

    s.push(1);
    while(!s.empty()) {
        int top = s.top();
        if(L[top].size()) {
            int aux = L[top][ L[top].size() - 1];
            L[top].pop_back();
            for(int j = 0; j < L[aux].size(); j ++) {
                if(L[aux][j] == top) {
                    L[aux][j] = L[aux][L[aux].size() - 1];
                    L[aux].pop_back();
                    j = oo;
                }
            }
            s.push(aux);
        }
        else {
            nr ++;
            stack<int> cif;
            while(top) cif.push(top % 10), top /= 10;
            while(!cif.empty()) out[++ k] = cif.top() + '0', cif.pop();
            out[++ k] = ' ';
            s.pop();
        }
    }
    if(nr != m + 1) {
        g << "-1\n";
        return 0;
    }
    out[-- k] = '\n';
    g << out;
    return 0;
}