Cod sursa(job #2051537)

Utilizator Alexandru_IulianAlexandru Iulian Alexandru_Iulian Data 29 octombrie 2017 10:18:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
#include <vector>

using namespace std;

int n,i,m,x,y;

vector <int> a[100001];
bool checked[100001];

ifstream f("dfs.in");
ofstream g("dfs.out");

void dfs(int x)
{
    checked[x]=1;
    vector <int>::iterator it;
    for(auto &it:a[x])
        if(!checked[it]) dfs(it);
}

int main()
{
    f>>n>>m;
    int nr=0;
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    for(i=1;i<=n;i++)
    if(checked[i]==false) nr++, dfs(i);

    g<<nr;
    return 0;
}