Cod sursa(job #2206714)

Utilizator EricEric Vilcu Eric Data 23 mai 2018 15:48:24
Problema Parcurgere DFS - componente conexe Scor 75
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,k;
struct muchie{int b;muchie *n;}*a[50002],*p;
bool v[100001];
void dfs(int x)
{
    v[x]=1;
    for(muchie *t=a[x];t!=NULL;t=t->n)
        if(!v[t->b])
            dfs(t->b);
}
int main()
{
    f>>n>>m;
    for(int i=1;i<=m;++i)
    {
        int A,B;
        f>>A>>B;
        p=new muchie;
        p->b=B;
        p->n=a[A];
        a[A]=p;
        p=new muchie;
        p->b=A;
        p->n=a[B];
        a[B]=p;
    }
    k=0;
    for(int i=1;i<=n;++i)if(!v[i]){++k;dfs(i);}
    g<<k;
}