Cod sursa(job #2230306)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 9 august 2018 17:44:57
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
vector <int> v[100005];
bitset <100005> c;
void f(int poz)
{
    if(c[poz])
        return;
    c[poz] = 1;
    for(int j = 0; j < v[poz].size(); j++)
        f(v[poz][j]);
}
int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);

    }
    int nr = 0;
    for(int i = 1; i <= n; i++)
    {
        if(c[i] == 0)
        {
            f(i);
            nr++;
        }
    }
    cout << nr;
    return 0;
}