Advertisement
vitormartinotti

Untitled

Nov 7th, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define MAX 100
  3.  
  4. using namespace std;
  5.  
  6. vector<int> grafo[MAX];
  7. int marc[MAX];
  8. int tamComp;
  9.  
  10. void dfs (int v){
  11.     tamComp++;
  12.     marc[v] = 1;
  13.     for (int i = 0; i < grafo[v].size(); i++){
  14.             int viz = grafo[v][i];
  15.             if (marc[viz] == 0){
  16.                 dfs(viz);
  17.             }
  18.     }
  19. }
  20.  
  21. int main (){
  22.     int n, m; scanf("%d %d", &n, &m);
  23.     for (int i = 1; i <= m; i++){
  24.         int a, b; scanf("%d %d", &a, &b);
  25.         grafo[a].push_back(b);
  26.         grafo[b].push_back(a);
  27.     }
  28.  
  29.     int nComp = 0;
  30.     int maxTamComp = 0;
  31.     for (int i = 0; i < n; i++){
  32.         if (marc[i] == 0){
  33.             nComp++;
  34.             tamComp = 0;
  35.             dfs(i);
  36.             if (tamComp > maxTamComp){
  37.                 maxTamComp = tamComp;
  38.             }
  39.         }
  40.     }
  41.  
  42.     printf("numero de componentes:%d tamanho maximo de uma componente: %d", nComp, maxTamComp);
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement