Cod sursa(job #3231295)

Utilizator AlexandruTigauTigau Alexandru AlexandruTigau Data 25 mai 2024 16:40:31
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
struct nod{
    int info;
    nod* urm;
};
nod* v[100001];
int viz[100001];
void dfs(int x)
{
    viz[x]=1;
    for(nod* p=v[x];p;p=p->urm)
        if(viz[p->info]==0)
            dfs(p->info);
}
int main()
{
   int x,y,n=0;
   while(f>>x>>y)
   {
        nod *p=new nod;
        p->info=y;
        p->urm=v[x];
        v[y]=p;
        n=max(n,x);
        n=max(n,y);
   }
   int cnt=0;
   for(int i=1;i<=n;i++)
   {
       if(viz[i]==0)
        dfs(i);
        cnt++;
   }
   g<<cnt/2;
    return 0;
}