Pagini recente » Cod sursa (job #2586118) | Cod sursa (job #1752036) | Cod sursa (job #1581405) | Cod sursa (job #1633062) | Cod sursa (job #2596220)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int NMAX=100001;
stack <int> stiva;
vector <int> graf[NMAX];
int n,m,f[NMAX];
void citire()
{
int x,y;
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>x>>y;
graf[x].push_back(y);
}
}
void dfs(int a)
{
int i;
stiva.push(a);
f[a]=1;
while(stiva.empty()==false)
{
int node=stiva.top();
stiva.pop();
int p=graf[node].size();
for(i=0;i<p;i++)
{
int current_node=graf[node][i];
f[current_node]=1;
stiva.push(current_node);
}
}
}
void afisare()
{
int k=0;
for(int i=1;i<=n;i++)
{
if(f[i]==0)
{
dfs(i);
k++;
}
}
out<<k;
}
int main()
{
citire();
afisare();
}