Cod sursa(job #920759)

Utilizator baicuviorelBaicu Viorel baicuviorel Data 20 martie 2013 17:00:31
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
#include <fstream>
#include <vector>
# define M 100005
using namespace std;
ifstream in ("dfs.in");
ofstream out ("dfs.out");
bool used[M];
vector<int> L[M];
int n,m;

void dfs(int x)
{
    used[x]=true;
    for(int i=0;i<L[x].size();i++)
    {
        int y = L[x][i];
        if(used[y]==false)
            dfs(y);
    }
}
int main()
{
    in>>n>>m;
    for(int i(1);i<=m;i++)
    {
        int x,y;
        in>>x>>y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    int Y=0;
    for(int i(1);i<=n;i++)
    {
        if(used[i]==false)
        {
            Y++;
            dfs(i);
        }
    }
    out<<Y;
}

/*#include <fstream>

using namespace std;
ifstream in ("dfs.in");
ofstream out ("dfs.out");
struct nod {int inf; nod *next;} *L[100001];
int n,m,used[100001],st[100001],k;
void ins(nod *&first , int inf)
{
    nod *p = new nod;
    p->inf = inf;
    p->next = first;
    first = p;
}
/*void l()
{
    for(int i=1;i<=k;i++)
    {
        nod *p=new nod;
        p=L[st[i]];
        while(p!=0)
        {
            int current_nod = p->inf;
            if(used[current_nod]==0)
            {
                st[++k]=current_nod;
                used[current_nod]++;
            }
            p=p->next;
        }
    }
}*//*
void l(int x)
{
    used[x]=1;
    nod * p =L[x];
    while(p!=0)
    {
        int c=p->inf;
        if(used[c]==0)
            l(c);
        p=p->next;
    }
}
int main()
{
    in>>n>>m;
    for(int i(1);i<=m;i++)
    {
        int x,y;
        in>>x>>y;
        ins(L[x],y);
        ins(L[y],x);
    }
    int Y=0;
    for(int i(1);i<=n;i++)
    if(used[i]==0){Y++; l(i);}
    out<<Y;
    return 0;
}
*/