Pagini recente » Cod sursa (job #237932) | Cod sursa (job #3159164) | Cod sursa (job #465546) | Cod sursa (job #1467988) | Cod sursa (job #907494)
Cod sursa(job #907494)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
vector <int> muchie[100000];
int n,m,co;
bool b[100000];
void citire(){
int i,x,y;
in>>n>>m;
for(i=1;i<=m;i++){
in>>x>>y;
muchie[x].push_back(y);
muchie[y].push_back(x);
}
}
void DFS(int y)
{
int j,x;
b[y]=true;
for(j=0;j<muchie[y].size();j++)
{
x=muchie[y][j];
if(b[x]==false)
{
b[x]=true;
DFS(x);
}
}
}
void componente(){
int i;
for(i=1;i<=n;++i){
if(b[i]==false){
co++;
DFS(i);
}
}
}
int main(){
citire();
componente();
out<<co;
return 0;
}