Cod sursa(job #2206276)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 22 mai 2018 09:08:37
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <cstdio>
#include <vector>
using namespace std;

vector <int> L[100001];
int f[100001];

void dfs(int i)
{
    f[i]=1;
    for(int it=0; it<L[i].size(); it++)
        if(!f[L[i][it]])
            dfs(L[i][it]);
}

int main()
{
    int n,m,rez,i,x,y;
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    scanf("%d%d",&n,&m);
    while(m--)
    {
        scanf("%d%d",&x,&y);
        L[x].push_back(y);
        L[y].push_back(x);
    }
    rez=0;
    for(i=1; i<=n; i++)
        if(!f[i])
        {
            rez++;
            dfs(i);
        }
    printf("%d",rez);

    return 0;
}