Cod sursa(job #2372819)

Utilizator zambi.zambyZambitchi Alexandra zambi.zamby Data 7 martie 2019 11:14:40
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

vector <int> graph[100000];
int viz[100000];
int nrc=1;

void DFS(int x)
{
    viz[x]=nrc;
    int i, lim=graph[x].size();
    for(i=1;i<=lim;i++)
        if(graph[x][i]==1 && viz[i]==0)
            DFS(i);
}

int main()
{
    ifstream f("graf.in");
    ofstream g("graf.out");
    int N, M, x, y;
    f>>N>>M;
    int i;
    for(i=1;i<=M;i++)
    {
        f>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(i=1;i<=N;i++)
    {
        if(viz[i]==0)
        {
            DFS(i);
            nrc++;
        }
    }
    g<<nrc<<endl;
    f.close();
    g.close();
    return 0;
}