Pagini recente » Statistici Mihalache Raul (mingiutza13) | Statistici BlackSheep (BlackSheep) | Monitorul de evaluare | Istoria paginii utilizator/costinoana | Cod sursa (job #1505087)
#include <fstream>
#include <vector>
using namespace std;
fstream fin("dfs.in", ios::in);
fstream fout("dfs.out", ios::out);
vector <int> g[100000];
int i, n, m;
void citire(){
int x, y;
fin >> n >> m;
for(i = 1; i <= n; i++)
g[i].push_back(0);
for(i = 1; i <= m; i++){
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
}
void DFS(int k){
g[k][0] = 1;
for(i = 1; i < (int) g[k].size(); i++){
int vecin = g[k][i];
if(!g[vecin][0])
DFS(vecin);
}
}
int main(){
int c = 0;
citire();
for(i = 1; i <= n; i++)
if(!g[i][0]){
c++;
DFS(i);
}
fout << c << '\n';
}