Cod sursa(job #2270546)

Utilizator isabellla128Haiura Andreea Isabela isabellla128 Data 27 octombrie 2018 11:30:27
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
#define MAX 100005

using namespace std;

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

int n, m, x, y, rez=0;
vector <int> G[MAX];
bool viz[MAX];

void dfs (int node)
{
    int i, newnode;
    viz[node]=true;
    for (i=0; i<G[node].size(); i++)
    {
        newnode=G[node][i];
        if (viz[newnode]==false)
                dfs(newnode);
    }
}

int main()
{
    int i, j;
    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for (i=1; i<=n; i++)
    {
        if(viz[i]==false)
        {
            rez++;
            dfs(i);
        }
    }
    cout<<rez;
    return 0;
}