Pagini recente » Cod sursa (job #2531600) | Cod sursa (job #607646) | Cod sursa (job #303269) | Cod sursa (job #2630067) | Cod sursa (job #1250477)
//#include <fstream>
#include <cstdio>
#include <vector>
#define dmax 100005
using namespace std;
//ifstream fin("ctc.in");
//ofstream fout("ctc.out");
FILE*fin=fopen("ctc.in", "r");
FILE*fout=fopen("ctc.out", "w");
vector <int> g[dmax];
vector <int> gt[dmax];
int n, m, postordine[dmax], nr;
bool uz[dmax];
void citire();
void rez();
void dfsG(int);
void dfsGt(int);
int main(){
citire();
rez();
return 0;
}
void citire(){
int x, y;
//fin>>n>>m;
fscanf(fin, "%d%d", &n, &m);
for(int i=1; i<=m; ++i){
//fin>>x>>y;
fscanf(fin, "%d%d", &x, &y);
g[x].push_back(y);
gt[y].push_back(x);
}
}
void rez(){
int i;
for(i=1; i<=n; ++i)
if(!uz[i])
dfsG(i);
for(i=1; i<=n; ++i) uz[i]=0;
for(i=n; i>0; --i)
if(!uz[postordine[i]]){
dfsGt(i);
//fout<<'\n';
fprintf(fout, "\n");
}
}
void dfsG(int vf){
uz[vf]=1;
int lg=g[vf].size();
for(int i=1; i<=lg; ++i)
if(!uz[ g[vf][i] ])
dfsG(g[vf][i]);
postordine[++nr]=vf;
}
void dfsGt(int vf){
uz[vf]=1;
//fout<<vf<<' ';
fprintf(fout, "%d ", vf);
int lg=gt[vf].size();
for(int i=1; i<=lg; ++i)
if(!uz[ gt[vf][i] ])
dfsGt(gt[vf][i]);
}