Advertisement
DimasDark

L4Q1

Aug 18th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5.  
  6. typedef struct nofila
  7. {
  8.     int i;
  9.     struct nofila* pai;
  10.     struct nofila* proximo;
  11.  
  12. } No;
  13.  
  14. typedef class fila
  15. {
  16.     public:
  17.     No* cabeca;
  18.     No* rabo;
  19.     int numElementos;
  20. public:
  21.     fila();
  22.     ~fila();
  23.     No* primeiro();
  24.     void insere(int i);
  25.     No* retira();
  26.     void imprimeFila();
  27.     bool isVazia();
  28.     void esvaziarFila();
  29.     int size();
  30.     bool existe(int i);
  31.  
  32. } Fila;
  33.  
  34. No* Fila::primeiro() {
  35.     No* p = this->cabeca;
  36.     return p;
  37. }
  38.  
  39. Fila::fila()
  40. {
  41.  
  42.     this->cabeca = this->rabo = NULL;
  43.     this->numElementos = 0;
  44. }
  45.  
  46.  
  47. //Insere no rabo
  48. void Fila::insere(int i)
  49. {
  50.     this->numElementos++;
  51.     No* novo = new No();
  52.     novo ->pai = this->cabeca;
  53.     novo ->i = i;
  54.     novo ->proximo = NULL;
  55.     if (!this->cabeca)
  56.     {
  57.         this->cabeca = this->rabo = novo;
  58.         return;
  59.     }
  60.  
  61.     this->rabo->proximo = novo;
  62.     this->rabo = novo;
  63.  
  64. }
  65.  
  66. Fila::~Fila()
  67. {
  68.  
  69.     while (this->numElementos > 0)
  70.     {
  71.         this->retira();
  72.     }
  73. }
  74.  
  75. //Retira da cabeça
  76. No* Fila::retira()
  77. {
  78.     if (!this->cabeca)
  79.     {
  80.         return NULL;
  81.     }
  82.     this->numElementos--;
  83.     No* ret = this->cabeca;
  84.  
  85.     if (this->cabeca == this->rabo)
  86.     {
  87.         this->cabeca = this->rabo = NULL;
  88.         return ret;
  89.     }
  90.     No* aux = this->cabeca;
  91.     this->cabeca = this->cabeca->proximo;
  92.     delete(aux);
  93.     return ret;
  94. }
  95.  
  96.  
  97. void Fila::imprimeFila()
  98. {
  99.     No* p;
  100.     for (p = this->cabeca; p != NULL; p = p->proximo)
  101.     {
  102.         if (p->proximo == NULL)
  103.             cout << "[" << p->i << "]";
  104.         else
  105.             cout << "[" << p->i << "]" << " ";
  106.     }
  107.  
  108. }
  109.  
  110. void Fila::esvaziarFila()
  111. {
  112.     while (this->numElementos > 0)
  113.         this->retira();
  114. }
  115.  
  116. bool Fila::isVazia()
  117. {
  118.     return this->cabeca == NULL;
  119. }
  120.  
  121. bool Fila::existe(int n)
  122. {
  123.     No* aux = this->cabeca;
  124.     while (aux != NULL)
  125.     {
  126.         if (aux->i == n)
  127.             return true;
  128.         aux = aux->proximo;
  129.     }
  130.     return false;
  131.  
  132. }
  133.  
  134. int Fila::size()
  135. {
  136.     return this->numElementos;
  137. }
  138.  
  139. int fazDFS(int v, Fila mapa[], int armazena[])
  140. {
  141.     int areas = 0;
  142.     if (mapa[v].numElementos == 0)
  143.         return 1;
  144.  
  145.     No* aux = mapa[v].cabeca;
  146. //    cout << mapa[v].cabeca->i;
  147.     while(aux)
  148.     {
  149.         if (armazena[aux->i] != -1)
  150.             areas += armazena[aux->i];
  151.         else
  152.             areas += fazDFS(aux->i, mapa, armazena);
  153.         aux = aux->proximo;
  154.     }
  155.     armazena[v] = areas;
  156.     return areas;
  157. }
  158.  
  159. int main()
  160. {
  161.     freopen("L4Q1.in", "r", stdin);
  162.     freopen("L4Q1.out", "w", stdout);
  163.  
  164.     int n; //n de areas
  165.     int k;
  166.     while (cin >> n)
  167.     {
  168.         if (n != 0)
  169.         {
  170.             int saida = 0;
  171.             int armazena[n];
  172.             Fila mapa[n];
  173.             for (int index = 0; index < n; index++)
  174.             {
  175.                 cin >> k;
  176.                 armazena[index] = -1;
  177.                 while (k > 0)
  178.                 {
  179.                     //Lê a matriz
  180.                     int num;
  181.                     cin >> num;
  182.                     mapa[index].insere(num);
  183.                     k--;
  184.                 }
  185.             }
  186.  
  187. //            for(int c = 0; c < n; c++)
  188. //            {
  189. //                mapa[c].imprimeFila();
  190. //                cout << endl;
  191. //            }
  192.  
  193.             saida = fazDFS(0, mapa, armazena);
  194.  
  195.  
  196. //            for(int w = 0; w < n; w++)
  197. //            {
  198. //                if (caminhos[w] == true)
  199. //                {
  200. //                    for (int v = 0; v < n; v++)
  201. //                    {
  202. //                        if (!caminhos[v])
  203. //                        {
  204. //                            int qtd = g.DFS(v, w);
  205. //                            armazena[w] += qtd;
  206. //                        }
  207. //                    }
  208. //                }
  209. //                else
  210. //                {
  211. //                    armazena[w] = 0;
  212. //                }
  213. //            }
  214.  
  215.             cout << "Saida: " << saida << endl;
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement