Cod sursa(job #1996136)

Utilizator zanugMatyas Gergely zanug Data 30 iunie 2017 12:24:48
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <vector>

#define pb push_back
#define ll long long

using namespace std;

const int N = 1e5 + 10;

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

int n, m;
vector < int > v[N];
bool b[N];

void dfs(int cnod)
{
    b[cnod] = true;
    for(int i = 0; i < v[cnod].size(); ++i)
    {
        int nnod = v[cnod][i];
        if(!b[nnod])
            dfs(nnod);
    }
}

int main()
{
    fin >> n >> m;
    int x, y;
    for(int i = 0; i < m; ++i)
    {
        fin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }

//    cout << 1;

    int s = 1;
    int ossz = 0;
    while(s <= n)
    {
        if(!b[s])
        {
            ++ossz;
            dfs(s);
        }
        ++s;
    }

    fout << ossz;

    return 0;
}