Pagini recente » Cod sursa (job #2850895) | Cod sursa (job #2956933) | Cod sursa (job #1215530) | Cod sursa (job #36627) | Cod sursa (job #2614727)
#include <bits/stdc++.h>
#define DAU ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
const string problem("dfs");
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
VVI g;
VB viz;
inline void DFS(int x) {
for (const int& y : g[x])
if (!viz[y])
viz[y] = true, DFS(y);
}
int n, m, x, y, nrcomp;
int main() {
DAU
fin >> n >> m;
g = VVI(n + 1);
for (int i = 1; i <= m; ++i) {
fin >> x >> y;
g[x].emplace_back(y);
}
viz = VB(n + 1);
for (int i = 1; i <= n; ++i)
if (!viz[i])
viz[i] = true, DFS(i), ++nrcomp;
fout << nrcomp;
PLEC
}