Pagini recente » Cod sursa (job #2409627) | Cod sursa (job #251095) | Cod sursa (job #576012) | Cod sursa (job #986189) | Cod sursa (job #627242)
Cod sursa(job #627242)
#include <stdio.h>
int n,m;
struct nod{
int y;
struct nod*next; }*g[50001],*a;
bool viz[50001];
void add(int x,int y){
nod*a=new nod;
a->y=y;
a->next=g[x];
g[x]=a;
}
void read_data(){
int i,x,y;
freopen("sortaret.in","r",stdin);
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
add(x,y);}
}
void DFS(int x){
viz[x]=1; nod*v=g[x];
while(v!=NULL){
if(!viz[v->y])DFS(v->y);
v=v->next;}
nod*c=new nod;
c->y=x; c->next=a; a=c;
}
void process(){
int i;
for(i=1;i<=n;i++)
if(!viz[i])DFS(i);
}
void write_data(){
freopen("sortaret.out","w",stdout);
while(a!=NULL){
printf("%d ",a->y);
a=a->next;};
}
int main(){
read_data();
process();
write_data();
}