Cod sursa(job #916299)

Utilizator ionutpPala Ionut ionutp Data 16 martie 2013 12:06:20
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

int N, M;
vector<int> V[100000];
int Conexe;
int P[100000];
void Read()
{
    fin >> N;
    fin >> M;
    int x, y;
    for(int i = 1; i <= M; i++)
    {
        fin >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }

}
void DF(int k)
{

    P[k] = 1;
    for(int i = 0; i < V[k].size(); i++)
    if(P[V[k][i]] == 0)
    DF(V[k][i]);


}
void Con()
{
    for(int i = 1; i <= N; i++)
    if(P[i] == 0)
    {
        Conexe++;
        DF(i);
    }
    fout << Conexe;
}
int main()
{
    Read();
    Con();
    fin.close();
    fout.close();
    return 0;
}