Cod sursa(job #2769818)
| Utilizator | Data | 17 august 2021 21:05:28 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 50 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int graph[1005][1005];
int n, m;
int vis[1005];
void dfs(int node)
{
vis[node] = true;
for (int i = 1; i <= n; ++i)
if (graph[node][i] and !vis[i])
dfs(i);
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
graph[x][y] = 1;
graph[y][x] = 1;
}
int cc = 0;
for (int i = 1; i <= n; ++i)
if (!vis[i])
{
dfs(i);
cc++;
}
fout << cc;
}
