Pagini recente » Cod sursa (job #1431094) | Cod sursa (job #2422784) | Cod sursa (job #3280724) | Cod sursa (job #2422684)
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
void DFS(int node, vector<vector<int>> adj, vector<bool> &viz)
{
viz[node] = 1;
fout << node << " ";
for (auto v : adj[node])
if (viz[v] == 0)
DFS(v, adj, viz);
}
int main()
{
int n, m, a, b, i, nr = 0;
fin >> n >> m;
vector<vector<int>> adj(n + 1);
for (i = 0; i < m; i++)
{
fin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<bool> viz(n + 1);
for (i = 1; i <= n; i++)
if (viz[i] == 0)
{
DFS(i, adj, viz);
fout << "\n";
nr++;
}
fout << nr;
return 0;
}