Lord_Samuk

Untitled

Feb 10th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Grafo{
  4.     int V;
  5.     list<int> *adj;
  6.  
  7. public:
  8.     Grafo (int V);
  9.     void adicionarAresta(int v1, int v2);
  10.     int bfs(int v);
  11. };
  12. Grafo::Grafo(int V){
  13.     this->V = V;
  14.     adj = new list<int>[V];
  15. }
  16. void Grafo::adicionarAresta(int v1, int v2){
  17.     adj[v1].push_back(v2);
  18. }
  19. int Grafo::bfs(int v){
  20.     queue<int> fila;
  21.     bool visitados[V];
  22.     int contador=0;
  23.     for(int i=0; i<V; i++){
  24.         visitados[i] = false;
  25.     }
  26.     visitados[v] = true;
  27.     while(true){
  28.         list<int>::iterator it;
  29.         for(it=adj[v].begin(); it!=adj[v].end(); it++){
  30.             if(!visitados[*it]){
  31.                 contador++;
  32.                 visitados[*it] = true;
  33.                 fila.push(*it);
  34.             }
  35.         }
  36.         if(!fila.empty()){
  37.             v = fila.front();
  38.             fila.pop();
  39.         }
  40.         else{
  41.             return contador;
  42.         }
  43.     }
  44. }
  45. int main(){
  46.    
  47.     int N, M, V, W, P, test, sum;
  48.     while(true){
  49.         sum = 0;
  50.         scanf("%d %d", &N, &M);
  51.         if(N==0 && M==0) return 0;
  52.         Grafo grafo(N);
  53.         test=M;
  54.         while(test--){
  55.             scanf("%d %d %d", &V, &W, &P);
  56.             grafo.adicionarAresta(V-1,W-1);
  57.             if(P==2) grafo.adicionarAresta(W-1,V-1);
  58.         }
  59.         for(int i=0; i<N; i++){
  60.             sum += grafo.bfs(i);
  61.         }
  62.         (sum/(N-1) == (N)) ? printf("1\n") : printf("0\n");
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment