Pagini recente » Cod sursa (job #939104) | Cod sursa (job #2677605) | Cod sursa (job #924682) | Cod sursa (job #1891811) | Cod sursa (job #2532174)
#define MAX_N 100000
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, ctc, t, timp[MAX_N + 1];
vector<int> G[MAX_N + 1];
int Df(int nod);
int main()
{
fin >> n >> m;
for (int i = 1, x, y; i <= m; ++i)
{
fin >> x >> y;
G[x].push_back(y);
}
for (int i = 1; i <= n; ++i)
{
if (timp[i] == 0)
{
Df(i);
}
}
fout << ctc;
fin.close();
fout.close();
return 0;
}
int Df(int nod)
{
int res = timp[nod] = ++t;
for (int vecin : G[nod])
{
if (timp[vecin] == 0)
{
res = min(res, Df(vecin));
}
else
{
res = min(res, timp[vecin]);
}
}
if (res == timp[nod])
{
++ctc;
}
return res;
}