Cod sursa(job #504892)

Utilizator mytzuskyMihai Morcov mytzusky Data 29 noiembrie 2010 13:48:42
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;
int n,m,viz[100];
struct graf
{
    int vc;
    graf*urm;
}*L[100005];
void add(graf *prim,int x)
{
    graf*p=new graf;
    p->vc=x;
    p->urm=prim;
    prim=p;
}

void citire()
{
    ifstream f("dfs.in");
    f>>n>>m;
    for(int i=1;i<=m;i++)
        {
            int x,y;
            f>>x>>y;
            add(L[x],y);
            add(L[y],x);
        }
}
void DFS(int x)
{
    viz[x]=1;
    for(graf*p=L[x];p;p=p->urm)
        if(!viz[p->vc])
            DFS(p->vc);
}


int main()
{
    ofstream g("dfs.out");
    citire();
    int nc=0;
    for(int i=1;i<=n;i++)
        if(viz[i]==0)
        {
            nc++;
            DFS(i);
        }
    g<<nc;
    return 0;
}