Cod sursa(job #550415)

Utilizator tudorDudeTudor Vioreanu tudorDude Data 9 martie 2011 15:07:08
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
# include <iostream>
# include <fstream>
# define nmax 100000
# include <vector>
using namespace std;
vector <int> g[nmax];
fstream f("dfs.in",ios::in);
fstream h("dfs.out",ios::out);
int n,m,i,k,viz[10000],nc,x,y;
void df(int k)
{
    int i,v;
    viz[k]=1;

    for(i=0;i<g[k].size();i++)
       { v=g[k][i];
           if (!viz[v]) df(i); }
}
int main()
{
    f>>n>>m;
   // scanf("%d%d", &x, &y);
    while (m--)
    {f>>x>>y;
    g[x].push_back(y);}
   /* for (i=1;i<=m;i++)
    {
        f>>x>>y;
        a[x][y]=a[y][x]=1;
    }
    f.close();*/
    for (i=1;i<=n;i++)
      {  if (viz[i]==0)
        {
            nc++;
            df(i);
        }}
    h<<nc;
    h.close();
    return 0;
}