Cod sursa(job #1240440)

Utilizator diana97Diana Ghinea diana97 Data 11 octombrie 2014 12:43:35
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <stack>

using namespace std;

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

const int NMAX = 262144 + 1;

int n;
bool este[NMAX];
int tata[NMAX];
stack <int> st;

bool rezolva() {
    int nod, ant, rad, nr = 1;
    f >> n >> rad;

    este[rad] = true;
    tata[rad] = -1;
    st.push(rad);
    ant = rad;

    while (f >> nod) {
        nr++;
        este[nod] = true;
        if (nod == tata[st.top()]) st.pop();
        else if (tata[nod] == 0) tata[nod] = st.top(), st.push(nod);
        ant = nod;
    }

    if (nr != 2 * n - 1) return false;
    for (int i = 1; i <= n; i++) if (este[i] == false) return false;
    if(st.size() > 1 || st.top() != rad) return false;

    tata[rad] = 0;
    return true;
}

void scrie() {
    for (int i = 1; i <= n; i++) g << tata[i] << ' ';
}

int main() {
    if (rezolva()) g << "DA\n", scrie();
    else g << "NU\n";
    return 0;
}