Cod sursa(job #2257088)

Utilizator Victor_InczeVictor Incze Victor_Incze Data 9 octombrie 2018 16:58:34
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
#define NMAX 100005

using namespace std;

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

int n, m, x, y, c;
vector <int> g[NMAX];
bitset <NMAX> v;

void dfs(int x)
{
    v[x]=1;
    for (int i=0; i<g[x].size(); i++)
        if (!v[g[x][i]])
            dfs(g[x][i]);
}

int main()
{
    in >> n >> m;
    for (int i=1; i<=m; i++)
    {
        in >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1; i<=n; i++)
        if (!v[i])
        {
            dfs(i);
            c++;
        }
    out << c;
    return 0;
}