Pagini recente » Cod sursa (job #751813) | Cod sursa (job #1840709) | Cod sursa (job #2200644) | Cod sursa (job #1179979) | Cod sursa (job #3316934)
{\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;\
\}\
}