Cod sursa(job #2261238)

Utilizator GhSamuelGherasim Teodor-Samuel GhSamuel Data 16 octombrie 2018 09:33:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
#define NMax 100005

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

int n, m, cnt;
vector <int> Graph[NMax];
bool viz[NMax];

void DFS(int node)
{
    viz[node] = true;
    for (int i = 0; i < Graph[node].size(); ++i)
        if (!viz[Graph[node][i]])
            DFS(Graph[node][i]);
}

//
int main()
{
    f >> n >> m;

    int x, y;
    for (int i = 1; i <= m; ++i) {
        f >> x >> y;
        Graph[x].push_back(y);
        Graph[y].push_back(x);
    }

    for (int i = 1; i <= n; ++i)
        if (!viz[i]) {
            ++cnt;
            DFS(i);
        }
    g << cnt;
    return 0;
}