Cod sursa(job #1415619)

Utilizator alex1096Postolache Alex alex1096 Data 5 aprilie 2015 16:02:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
const int nmax=100010;
int N,M;
bool poz[nmax];
vector<int> v[nmax];
void citire();
void conex(int);
int DFS();
void afisare();
void citire()
{
    int x,y;
    ifstream fin("dfs.in");
    fin>>N>>M;
    while(M--)
    {
        fin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    fin.close();
}
void conex(int j)
{
    poz[j]=true;
    for(vector<int>::iterator it=v[j].begin();it!=v[j].end();++it)
        if(poz[*it]==false)
            conex(*it);
}
int DFS()
{
    int nr=0;
    for(int i=1;i<=N;++i)
        if(poz[i]==false)
    {
        nr++;
        conex(i);
    }
    return nr;
}
void afisare()
{
    ofstream fout("dfs.out");
    fout<<DFS();
    fout.close();
}
int main()
{
    citire();
    afisare();
    return 0;
}