Pagini recente » Cod sursa (job #1448875) | Cod sursa (job #1257639) | Cod sursa (job #2787026) | Cod sursa (job #2980336) | Cod sursa (job #2782540)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int nlim = 100005;
int n, m, conex_components;
bool visited[nlim];
vector <int> edges[nlim];
void DFS(int node){
visited[node] = true;
for(int i = 0; i < edges[node].size(); i++){
int adj = edges[node][i];
if(!visited[adj])
DFS(adj);
}
}
void read(){
fin >> n >> m;
for(int i = 1; i <= m; i++){
int x, y;
fin >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
for(int i=1;i<=n;i++){
if(!visited[i])
conex_components++;
DFS(i);
}
fout<<conex_components;
}
int main(){
read();
}