Cod sursa(job #1877409)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 13 februarie 2017 12:16:42
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;

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

constexpr int maxn = 1e5 + 10;
vector<int> vec[maxn];
bitset<maxn> viz = 0;

void dfs(const int cur){
    viz[cur] = 1;
    for(const auto next : vec[cur])
        if(!viz[next]) dfs(next); }

int main()
{
    int n, m;
    f >> n >> m;
    for(int i = 0, x, y; i < m; ++i)
        f >> x >> y, vec[x].push_back(y), vec[y].push_back(x);
    int rez = 0;
    for(int i = 1; i <= n; ++i)
        if(!viz[i]) ++rez, dfs(i);
    g << rez << endl;
    return 0; }