Pagini recente » Cod sursa (job #2675699) | Cod sursa (job #620007) | Cod sursa (job #933454) | Cod sursa (job #2928206) | Cod sursa (job #2594025)
#include <fstream>
#include <vector>
using namespace std;
const int maxn = 100005;
ifstream cin("dfs.in");
ofstream cout("dfs.out");
int N, M;
vector < vector <int> > G;
bool viz[maxn];
void citire()
{
cin >> N >> M;
int x, y;
G.resize(maxn + 5);
for(int i = 1 ; i <= M ; i++)
cin >> x >> y, G[x].push_back(y), G[y].push_back(x);
}
void DFS(int poz)
{
if(viz[poz])
return;
viz[poz] = 1;
for(int i = 0 ; i < G[poz].size() ; i++)
DFS(G[poz][i]);
}
void rez()
{
int nr = 0;
for(int i = 1 ; i <= N ; i++)
if(!viz[i])
{
nr++, DFS(i);
/*for(int j = 1 ; j <= N ; j++)
cout << j << ' ' << viz[j] << '\n';
cout << '\n';*/
}
cout << nr;
}
int main()
{
citire();
rez();
return 0;
}