Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- typedef struct nofila
- {
- int i;
- struct nofila* pai;
- struct nofila* proximo;
- } No;
- typedef class fila
- {
- public:
- No* cabeca;
- No* rabo;
- int numElementos;
- public:
- fila();
- ~fila();
- No* primeiro();
- void insere(int i);
- No* retira();
- void imprimeFila();
- bool isVazia();
- void esvaziarFila();
- int size();
- bool existe(int i);
- } Fila;
- No* Fila::primeiro() {
- No* p = this->cabeca;
- return p;
- }
- Fila::fila()
- {
- this->cabeca = this->rabo = NULL;
- this->numElementos = 0;
- }
- //Insere no rabo
- void Fila::insere(int i)
- {
- this->numElementos++;
- No* novo = new No();
- novo ->pai = this->cabeca;
- novo ->i = i;
- novo ->proximo = NULL;
- if (!this->cabeca)
- {
- this->cabeca = this->rabo = novo;
- return;
- }
- this->rabo->proximo = novo;
- this->rabo = novo;
- }
- Fila::~Fila()
- {
- while (this->numElementos > 0)
- {
- this->retira();
- }
- }
- //Retira da cabeça
- No* Fila::retira()
- {
- if (!this->cabeca)
- {
- return NULL;
- }
- this->numElementos--;
- No* ret = this->cabeca;
- if (this->cabeca == this->rabo)
- {
- this->cabeca = this->rabo = NULL;
- return ret;
- }
- No* aux = this->cabeca;
- this->cabeca = this->cabeca->proximo;
- delete(aux);
- return ret;
- }
- void Fila::imprimeFila()
- {
- No* p;
- for (p = this->cabeca; p != NULL; p = p->proximo)
- {
- if (p->proximo == NULL)
- cout << "[" << p->i << "]";
- else
- cout << "[" << p->i << "]" << " ";
- }
- }
- void Fila::esvaziarFila()
- {
- while (this->numElementos > 0)
- this->retira();
- }
- bool Fila::isVazia()
- {
- return this->cabeca == NULL;
- }
- bool Fila::existe(int n)
- {
- No* aux = this->cabeca;
- while (aux != NULL)
- {
- if (aux->i == n)
- return true;
- aux = aux->proximo;
- }
- return false;
- }
- int Fila::size()
- {
- return this->numElementos;
- }
- int fazDFS(int v, Fila mapa[], int armazena[])
- {
- int areas = 0;
- if (mapa[v].numElementos == 0)
- return 1;
- No* aux = mapa[v].cabeca;
- // cout << mapa[v].cabeca->i;
- while(aux)
- {
- if (armazena[aux->i] != -1)
- areas += armazena[aux->i];
- else
- areas += fazDFS(aux->i, mapa, armazena);
- aux = aux->proximo;
- }
- armazena[v] = areas;
- return areas;
- }
- int main()
- {
- freopen("L4Q1.in", "r", stdin);
- freopen("L4Q1.out", "w", stdout);
- int n; //n de areas
- int k;
- while (cin >> n)
- {
- if (n != 0)
- {
- int saida = 0;
- int armazena[n];
- Fila mapa[n];
- for (int index = 0; index < n; index++)
- {
- cin >> k;
- armazena[index] = -1;
- while (k > 0)
- {
- //Lê a matriz
- int num;
- cin >> num;
- mapa[index].insere(num);
- k--;
- }
- }
- // for(int c = 0; c < n; c++)
- // {
- // mapa[c].imprimeFila();
- // cout << endl;
- // }
- saida = fazDFS(0, mapa, armazena);
- // for(int w = 0; w < n; w++)
- // {
- // if (caminhos[w] == true)
- // {
- // for (int v = 0; v < n; v++)
- // {
- // if (!caminhos[v])
- // {
- // int qtd = g.DFS(v, w);
- // armazena[w] += qtd;
- // }
- // }
- // }
- // else
- // {
- // armazena[w] = 0;
- // }
- // }
- cout << "Saida: " << saida << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement