Cod sursa(job #2535767)

Utilizator cioraDunca Raul ciora Data 1 februarie 2020 11:14:46
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#define M 100001
using namespace std;
ifstream f ("dfs.in");
ofstream g ("dfs.out");
struct nod
{
    int x;
    nod*urm;
};
nod*L[M];
nod*p;
int viz[M],i,x,y,m,n,k;
void adaugare (int i,int j)
{
    nod*p;
    p=new nod;
    p->x=j;
    p->urm=L[i];
    L[i]=p;
}
void DFS(int nor)
{
    nod*p;
    viz[nor]=1;
    p=L[nor];
    while(p)
    {
        if(viz[p->x]==0)
            DFS(p->x);
        p=p->urm;

    }
}
int main()
{
 f>>n>>m;
for(i=1;i<=m;i++)
 {
     f>>x>>y;
     adaugare(x,y);
     adaugare(y,x);
 }
  for(i=1;i<=n;i++)
     if(viz[i]==0)
     {
         k++;
         DFS(i);
     }
g<<k;
    return 0;
}