Cod sursa(job #3248557)
Utilizator | Data | 12 octombrie 2024 10:36:23 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 5 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.59 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, x, y, rez = 1;
vector<int> graph[1000];
int marked[1000];
void dfs(int k){
int lim = graph[k].size();
for(int i = 0; i<lim; i++){
if(marked[ graph[k][i] ] == 0){
marked[ graph[k][i] ] = 1;
rez++;
dfs(i);
}
}
}
int main()
{
fin>>n>>m;
for(int i = 0; i<n; i++){
fin>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(1);
fout<<rez;
return 0;
}