GastonFontenla

N3P4 - Correo Central (múltiples DFS)

Sep 1st, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool M[1001][1001];
  6. vector <vector <int> > ady;
  7.  
  8. void DFS(const int &origen, int nodo)
  9. {
  10.     M[origen][nodo] = true;
  11.  
  12.     for(const auto i:ady[nodo])
  13.         if(M[origen][i] == false)
  14.             DFS(origen, i);
  15. }
  16.  
  17. int correocentral(int n, vector <int> a, vector <int> b)
  18. {
  19.     /**
  20.     Solución que apunta a subtareas 1, 2, 3
  21.     solo en caso que la respuesta sea n
  22.     80% de 90 puntos = 72 puntos
  23.    
  24.     Complejidad: O(n*DFS) = O(n*(n+m)) =~ 201.000.000
  25.     **/
  26.  
  27.     ady = vector <vector <int> > (n+1);
  28.  
  29.     for(int i=0; i<a.size(); i++)
  30.         ady[a[i]].push_back(b[i]);
  31.  
  32.     for(int i=1; i<=n; i++)
  33.         DFS(i, i);
  34.  
  35.     bool todosUnos = true;
  36.  
  37.     for(int i=1; i<=n; i++)
  38.         for(int j=1; j<=n; j++)
  39.             if(M[i][j] == false)
  40.                 todosUnos = false;
  41.  
  42.     if(todosUnos)
  43.         return n;
  44.     return 0;
  45. }
  46.  
  47. /**
  48. //Función main auxiliar para testear
  49. int main()
  50. {
  51.     int n, m;
  52.     cin >> n >> m;
  53.  
  54.     vector <int> a(m), b(m);
  55.  
  56.     for(int i=0; i<m; i++)
  57.         cin >> a[i] >> b[i];
  58.  
  59.     cout << correocentral(n, a, b) << endl;
  60.  
  61.     return 0;
  62. }
  63. **/
Advertisement
Add Comment
Please, Sign In to add comment