Pagini recente » Cod sursa (job #805159) | Cod sursa (job #398795) | Cod sursa (job #2299328) | Istoria paginii runda/boolanizarea/clasament | Cod sursa (job #2424306)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector < vector <int> > G;
vector <int> viz;
int N,M;
stack <int> stiva;
void DFS(int nod)
{
viz[nod]=1;
stiva.push(nod);
vector<int>::iterator it;
while(stiva.empty()==0)
{
int aux;
aux=stiva.top();
stiva.pop();
for(it=G[aux].begin();it!=G[aux].end();it++)
if(viz[*it]==0)
stiva.push(*it);
}
}
int main()
{
fin>>N>>M;
G.resize(N+1);
viz.resize(N+1,0);
int i;
for(i=1;i<=M;i++)
{
int x,y;
fin>>x>>y;
G[x].push_back(y);
}
for(i=1;i<=N;i++)
{
if(viz[i]==0)
{
DFS(i);
fout<<i<<" ";
}
}
return 0;
}