Cod sursa(job #1717685)

Utilizator stefanchistefan chiper stefanchi Data 15 iunie 2016 16:06:58
Problema Parcurgere DFS - componente conexe Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <stdio.h>
#include <vector>
#define N 100005
using namespace std;
int n, m;
vector <int> g[N];
bool vis[N];

void citire()
{
    int x , y;
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    scanf("%d %d", &n , &m);
    for(int i = 0 ; i < m ; i++)
    {
        scanf("%d %d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

void dfs(int x)
{
   vector <int> :: iterator it;
    vis[x] = true;
    for(it = g[x].begin(); it != g[x].end(); ++it)
    {
        if(!vis[*it])
            dfs(*it);
    }
}

void parcurgere()
{
    int raspuns = 0 ;

    for(int i = 0 ; i < n ; i++)
    {
        if(!vis[i])
        {
            dfs(i);
            raspuns++;
        }
    }
    printf("%d",raspuns);
}


int main()
{
     citire();
     parcurgere();
    return 0;
}