Advertisement
kokokozhina

i tried 6_dnmc

Mar 29th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct stack{
  8. int inf;
  9. stack* next;
  10. };
  11.  
  12. stack *init(){
  13. return NULL;
  14. }
  15.  
  16. void push(stack *&h, int x){
  17. stack *top = new stack();
  18. top->inf = x;
  19. top->next = h;
  20. h = top;
  21. }
  22.  
  23. int top(stack *&h){
  24. return h->inf;
  25. }
  26.  
  27. int pop(stack *&h){
  28. int i = h->inf;
  29. stack *cur = h;
  30. h=h->next;
  31. delete cur;
  32. return i;
  33. }
  34.  
  35. int main(){
  36. #ifdef _DEBUG
  37. freopen("input.txt", "r", stdin);
  38. freopen("output.txt", "w", stdout);
  39. #endif
  40. int n;
  41. cin >> n;
  42. vector<vector<int>> v(n, vector<int> (n));
  43. vector<bool> used(n, false);
  44. cout << "Fill in the matrix of adjacency: press 1 for yes, 0 for no\n";
  45. for(int i = 0; i < n; i++){
  46. for(int j = 0; j < n; j++){
  47. cin >> v[i][j];
  48. }
  49. }
  50. stack *h = init();
  51. stack *res = init();
  52. for(int i = 0; i < n; i++){
  53. push(h, i);
  54. while(h){
  55. int cur = top(h);
  56. //if(used[cur]){
  57. // pop(h);
  58. // break;
  59. //}
  60. bool flag = false;//shows true if there are no unused tops left
  61. int j = 0;
  62. while(j < n){
  63. if(v[cur][j] && !used[j]){
  64. push(h, cur);
  65. break;
  66. }
  67. else j++;
  68. }
  69. if(flag){
  70. used[cur] = true;
  71. pop(h);
  72. push(res, pop(h));
  73. }
  74. }
  75. }
  76. while(res){
  77. cout << pop(res) << " ";
  78. }
  79.  
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement