Cod sursa(job #1782013)

Utilizator gamanedyGaman Eduard-Marian gamanedy Data 17 octombrie 2016 18:37:12
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>

using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,viz[100002];
struct nod
{
    int v;
    nod *urm;
};
nod *L[100002], *p;
void fill(int x)
{
    viz[x]=1;
    for(nod *p=L[x];p!=0;p=p->urm)
    {
        int y=p->v;
        if(viz[y]==0)
        {
            fill(y);
        }
    }
}
int i,x,y,c;
int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++)
    {
        fin>>x>>y;
        p=new nod;
        p->v=y;
        p->urm=L[x];
        L[x]=p;
        p=new nod;
        p->v=x;
        p->urm=L[y];
        L[y]=p;
    }
    c=0;
    for(i=1;i<=n;i++)
    {
        if(viz[i]==0)
        {
            c++;
            fill(i);
        }
    }
    fout<<c;
    return 0;
}