Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sofhiasouza
Last active September 20, 2019 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sofhiasouza/7282b1c1be00d3d5cfd48d0aacfb975d to your computer and use it in GitHub Desktop.
Save sofhiasouza/7282b1c1be00d3d5cfd48d0aacfb975d to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10; //declaro maxn como o valor maximo do meu n
int n, m, vist[maxn], qtd; //vist é meu vetor de visitados, que começa zerado
vector < int > grafo[maxn];
void dfs(int u)
{
vist[u] = 1;
qtd++; //pra cada vértice que eu visito, é um vértice a mais no tamanho da minha componente
for(int i = 0 ; i < grafo[u].size() ; i++)
{
int v = grafo[u][i];
if(!vist[v]) dfs(v);
}
}
int main()
{
cin >> n >> m;
for(int i = 0 ; i < m ; i++)
{
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
int maior = 0; //variável onde irei guardar o tamanho da maior componente
for(int i = 1 ; i <= n ; i++)
{
qtd = 0; //qtd é a variável que irei usar para contar a quantidade de vértices existentes na componente
if(!vist[i])
{
dfs(i);
maior = max(maior, qtd); //se o tamanho dessa componente for maior do que a que eu tinha guardado antes, substituo
}
}
cout << maior << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment