Pagini recente » Cod sursa (job #3260706) | Cod sursa (job #1031393) | Cod sursa (job #2425436) | Cod sursa (job #624504) | Cod sursa (job #3205148)
#include <fstream>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
struct Node{
int data;
struct Node *next;
};
int n, m, k =0;
struct Node *List[50001];
int used[50001], v[50001];
void read_graph() {
fin >> n >> m;;
int i, j;
for (int x = 1; x <= m; x++) {
fin >> i >> j;
struct Node *c = new Node;
c -> data = j;
c -> next = List[i];
List[i] = c;
}
}
void dfs(int node) {
fout << node << " ";
used[node] = 1;
struct Node *c = List[node];
while (c) {
if (!used[c->data]) {
dfs(c->data);
}
c = c -> next;
}
}
// void read() {
// fin >> n >> m;
// for (int i = 1; i <= m; i++) {
// int i, j; fin >> i >> j;
// matrix[i][j] = 1;
// }
// }
//
// void dfs() {
//
// }
int main() {
read_graph();
for (int node = 1; node <= n; node++)
if (!used[node])
dfs(node);
return 0;
}