Pagini recente » Statistici Serghiuta Alexia (AlexiaSerghiuta) | Cod sursa (job #2396364) | Statistici Coroban Silviu Iulian (Iulian.Coroban) | Cod sursa (job #1425204) | Cod sursa (job #1705838)
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> graph[100005];
vector <bool> viz(100005, false);
void dfs(int node){
viz[node] = true;
int son;
for(int i = 0; i < graph[node].size(); i++){
son = graph[node][i];
if(!viz[son])
dfs(son);
}
}
int main(){
int n, m;
f >> n >> m;
int x, y;
for(int i = 1; i <= m; i++){
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int nr = 0;
for(int i = 1; i <= n; i++){
if(!viz[i]){
dfs(i);
nr ++;
}
}
g << nr;
}