Cod sursa(job #2321094)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 15 ianuarie 2019 18:02:00
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
#define NMAX 100001
using namespace std;

ifstream in("dfs.in");
ofstream out("dfs.out");

int viz[NMAX];
list<int> *l;

void dfs(int nod)
{
    viz[nod]++;

    for(auto it = l[nod].begin(); it != l[nod].end(); it++)
        if(viz[*it] == 0)
        {
            dfs(*it);
        }
}

int main()
{
    int n, m;
    in >> n >> m;

    l = new list<int>[n+1];

    while(m--)
    {
        int x, y;
        in >> x >> y;

        l[x].push_back(y);
        l[y].push_back(x);
    }

    int cc = 0;

    for(int i = 1; i <= n; i++)
        if(viz[i] == 0)
        {
            viz[i] = 1;
            if(!l[i].size())
            {
                cc++;
                continue;
            }

            dfs(i);
            if(viz[i] == 2) cc++;
        }

    out << cc;

    return 0;
}