Pagini recente » Cod sursa (job #574244) | Cod sursa (job #2625064) | Cod sursa (job #299247) | Cod sursa (job #1628874) | Cod sursa (job #3123679)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
void read_input()
{
}
void write_output()
{
}
void solve(vector<vector<int>> graf, vector<int> &visited, int nod_curent)
{
for(int j = 0; j < graf[nod_curent].size(); j++) {
if(!visited[graf[nod_curent][j]]) {
visited[graf[nod_curent][j]] = 1;
solve(graf, visited, graf[nod_curent][j]);
}
}
}
int main()
{
int n;
int m;
int x, y;
f >> n >> m;
vector<vector <int>> graf(n + 1);
vector<int> visited(n + 1, 0);
for(int i = 0; i < m; i++) {
f >> x >> y;
graf[x].push_back(y);
}
int nmb = 0;
for(int i = 1; i <= n; i++) {
if(!visited[i]) {
solve(graf, visited, i);
nmb++;
}
}
g<<nmb;
}