Cod sursa(job #2346210)

Utilizator VarticeanNicolae Varticean Varticean Data 17 februarie 2019 13:23:26
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;

typedef struct nod
{
    int val;
    nod* next;
}* graf;

bitset <100100> vis;
int n,m,cnt;
graf lta[100100];

void add(graf &a, int v )
{
    graf b = new nod;
    b->val=v;
    b->next=a;
    a=b;
}

void dfs(int n)
{
    vis[n]=1;
    for(graf x = lta[n]; x!=NULL; x=x->next)
        if(!vis[x->val]) dfs(x->val);
}
int main()
{
    ifstream in("dfs.in");
    ofstream out("dfs.out");
    ios::sync_with_stdio(0);
    in >> n >> m;
    int x,y;
    for(int i=1; i<=m; i++)
    {
        in >> x >> y;
        add(lta[x],y);
    }

    for(int i=1; i<=n; i++)
        if(!vis[i])
    {
        cnt++;
        dfs(i);
    }
    out << cnt;


    return 0;
}