fork download
  1. /*
  2.   Copyright 2011 Marek "p2004a" Rusinowski
  3.   Tarjan algorithm
  4. */
  5. #include <cstdio>
  6. #include <vector>
  7.  
  8. #define MAXN 1000000
  9.  
  10. std::vector<int> edges[MAXN];
  11. bool visited[MAXN];
  12. std::vector<int> sorted;
  13.  
  14. void dfs(int v) {
  15. visited[v] = true;
  16. for (unsigned i = 0; i < edges[v].size(); ++i) {
  17. if (!visited[edges[v][i]]) {
  18. dfs(edges[v][i]);
  19. }
  20. }
  21. sorted.push_back(v);
  22. }
  23.  
  24. int main() {
  25. int a, b, n, m;
  26. scanf("%d %d", &n, &m);
  27. for (int i = 0; i < m; ++i) {
  28. scanf("%d %d", &a, &b);
  29. edges[--a].push_back(--b);
  30. }
  31. for (int i = 0; i < n; ++i) {
  32. if (!visited[i]) {
  33. dfs(i);
  34. }
  35. }
  36. for (int i = n - 1; i >= 0; --i) {
  37. printf("%d ", sorted[i] + 1);
  38. }
  39. printf("\n");
  40. return 0;
  41. }
  42.  
stdin
6 6
1 2
2 4
2 5
4 5
3 6
2 6
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
3 1 2 6 4 5