Cod sursa(job #1996131)

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

#define pb push_back
#define ll long long

using namespace std;

const int N = 1e5 + 10;
const int M = 2e5 + 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)
    {
        ++ossz;
        dfs(s);
        s = 0;
        for(int i = 1; i <= n && !s; ++i)
        {
            if(b[i] == 0)
                s = i;
        }
    }

    fout << ossz;

    return 0;
}