Cod sursa(job #2326513)

Utilizator SilvestruState Silvestru Nicolae Silvestru Data 23 ianuarie 2019 16:53:53
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int N = 100001;

vector<int> a[N];

int m, n, nr;
bool viz[N];
void citire ()
{
    int x, y;
    in>>n>>m;
    for (int i=0; i<m; i++)
    {
        in>>x>>y;
        a[x].push_back(y);
        a[y].push_back(y);
    }
    in.close();
}

void dfs(int x)
{
    viz[x]=true;
    for (auto y:a[x])
    {
        if (!viz[y])
            dfs(y);
    }
}

int main()
{
    int nc=0;
    citire();
    for (int i=1; i<=n; i++)
    {
        if (!viz[i])
        {
            nc++;
            dfs(i);
        }
    }
    out<<nc;
    return 0;
}