Graph / trees- Number of children - C ++

0

I have a problem in the code below, I wonder if all the vertices of each level have the same number of children, but something is going wrong.

#include <iostream>
#include <vector>

using namespace std;

vector<int> lista[100100];
int qtde, a, b;

bool DFS(int x){ //vertice que está sendo analisado(pai);

    if(x == qtde) return true; //se passar da ult. folha, está balanceado;

    if((int)lista[x].size() == 1) return DFS(x+1); //se o pai só tem 1 vertice, verifica o prox. vertice
    else
    {
        //caso contrario, verifica a qtde de vertices q o sucessor tem, verificando com o do lado;
        //caso seja diferente, retorna false;
        for(int i = 1; i < (int)lista[x].size(); i++){

            int pai = lista[x][i];

            if((int)lista[pai].size() != (int)lista[pai-1].size()) 
                return false;
        }

        return DFS(x+1);
    }
}

int main(){

    cin >> qtde;
    for(int i = 1; i <= qtde; i++){
        cin >> a >> b;

        lista[b].push_back(a);
    }

    if(DFS(0)) cout << "bem" << endl;
    else
        cout << "mal" << endl;
}
    
asked by anonymous 23.08.2018 / 20:17

0 answers