Pagini recente » Cod sursa (job #1474031) | Monitorul de evaluare | Cod sursa (job #313933) | Cod sursa (job #2741378) | Cod sursa (job #2176294)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n,m,x,y,viz[100001],c;
vector<int> g[100001], gt[100001];
stack<int>s;
void dfs(int nod)
{
viz[nod]=1;
for (auto v: g[nod])
if (!viz[v])
dfs(v);
s.push(nod);
}
void dfsr(int nod)
{
viz[nod]=1;
for (auto v :gt[nod])
if (!viz[v])
dfsr(v);
}
int main()
{
fin>>n>>m;
for (int i=1;i<=m;i++)
{
fin>>x>>y;
g[x].push_back(y);
gt[y].push_back(x);
}
for (int i=1;i<=n;i++)
if (!viz[i])
dfs(i);
memset(viz,0,sizeof(viz));
while (!s.empty())
{
int t=s.top();
s.pop();
if (!viz[t])
dfsr(t), c++;
}
fout<<c;
return 0;
}