Cod sursa(job #1108218)

Utilizator dr_personalityEftime Andrei Horatiu dr_personality Data 15 februarie 2014 15:03:39
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");

vector <int> g[100002];
int n, m, x, y, rasp;
bool poz[100002];

void dfs(int a){
    poz[a] = 1;
    for(int i = 0; i<g[a].size(); i++)
    {
        g[a][i] = 1;
        dfs(g[a][i]);
    }
}

int main()
{
    in>>n>>m;
    for(int i = 0; i<m; i++)
    {
        in>>x>>y;

        g[x].push_back(0);
        g[x][g[x].size] = y;

        g[y].push_back(0);
        g[y][g[y].size] = x;
    }

    for(int i = 1; i<=n; i++)
    {
        if(poz[i]==0)
        {
            dfs(i);
            rasp++;
        }

    }

    out<<rasp<<'\n';
    return 0;
}