Cod sursa(job #2080200)

Utilizator Claudiu07Pana Claudiu Claudiu07 Data 2 decembrie 2017 16:02:42
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
#define maxn 100010

int N, M, L, nr;
vector <int> A[maxn];
bool ok[maxn];

void DFS(int i)
{
    ok[i]=1;
    for(int j=0; j<A[i].size(); j++)
        if(ok[A[i][j]]==0)
            DFS(A[i][j]);
}
int main()
{
    f>>N>>M;
    int x,y;
    for(int i=1; i<=M; i++)
    {
        f>>x>>y;
        A[x].push_back(y);
        A[y].push_back(x);
    }
    for(int i=1; i<=N; i++)
        if(ok[i]==0)
        {
            nr++;
            DFS(i);
        }
    g<<nr;
    return 0;
}