Cod sursa(job #3316934)

Utilizator Dana_VidroiuVidroiu Dana-Maria Dana_Vidroiu Data 21 octombrie 2025 14:29:35
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 1.03 kb
{\rtf1\ansi\ansicpg1252\cocoartf2709
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0

\f0\fs24 \cf0 #include <iostream>\
#include <vector>\
using namespace std;\
\
const int MAXN = 100000;\
\
vector<int> G[MAXN + 1];\
bool viz[MAXN + 1];\
int N, M;\
\
void dfs(int nod) \{\
    viz[nod] = true;\
    for (int vecin : G[nod])\
        if (!viz[vecin])\
            dfs(vecin);\
\}\
\
int main() \{\
    cin >> N >> M;\
    for (int i = 0; i < M; i++) \{\
        int x, y;\
        cin >> x >> y; \
        G[x].push_back(y);\
        G[y].push_back(x);\
    \}\
\
    int componente = 0;\
    for (int i = 1; i <= N; i++)\
        if (!viz[i]) \{\
            componente++;\
            dfs(i);\
        \}\
\
    cout << componente << "\\n";\
    return 0;\
\}\
}