Cod sursa(job #905755)

Utilizator radu_bucurRadu Bucur radu_bucur Data 6 martie 2013 09:33:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
bool b[100001];
vector <int> a[100001];
int z, p, i, m, n, nr;
void dfs(int y)
{
    int j,x;
    b[y]=true;
    for(j=0;j<a[y].size();j++)
    {
        x=a[y][j];
        if(b[x]==false)
        {
            b[x]=true;
            dfs(x);
        }
    }
}
int main()
{
    in>>n>>m;
    for(i=1;i<=m;i++)
    {
        in>>z>>p;
        a[z].push_back(p);
        a[p].push_back(z);
    }

    for(i=1;i<=n;i++)
    {
        if(b[i]==false)
        {
            nr++;
            b[i]=true;
            dfs(i);
        }
    }
    out<<nr;
    return 0;
}