Pagini recente » Cod sursa (job #2911326) | Istoria paginii runda/inca_vacanta_ix/clasament | Istoria paginii runda/sth_cute | Cod sursa (job #596553) | Cod sursa (job #2581102)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("sortaret.in");
ofstream f2("sortaret.out");
struct Nod{
int info;
Nod * next;
};
int viz[100005], n, m, x, y, i;
Nod listadeAdiacenta[100005];
int control[100005];
void adaugare(Nod * prim, int z){
Nod * newNode = new Nod;
newNode -> info = z;
newNode -> next = NULL;
Nod * it = prim;
while(it -> next != NULL){
it = it-> next;
}
it -> next = newNode;
}
void dfs(int plecare){
if(viz[plecare] == 1){
return;
}
viz[plecare] = 0;
Nod * it = listadeAdiacenta[plecare].next;
while(it != NULL){
dfs(it -> info);
it = it -> next;
}
f2 << plecare << " ";
viz[plecare] = 1;
}
int main()
{
f >> n >> m;
for(i = 1; i <= n; i++){
viz[i] = -1;
}
for(i = 1; i <= m; i++){
f >> x >> y;
Nod * listaX = &listadeAdiacenta[x];
adaugare(listaX, y);
}
for(i = 1; i <= n; i++){
if(viz[i] == 0 || viz[i] == -1){
dfs(i);
}
}
return 0;
}