Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- using namespace std;
- struct stack{
- int inf;
- stack* next;
- };
- stack *init(){
- return NULL;
- }
- void push(stack *&h, int x){
- stack *top = new stack();
- top->inf = x;
- top->next = h;
- h = top;
- }
- int top(stack *&h){
- return h->inf;
- }
- int pop(stack *&h){
- int i = h->inf;
- stack *cur = h;
- h=h->next;
- delete cur;
- return i;
- }
- int main(){
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- vector<vector<int>> v(n, vector<int> (n));
- vector<bool> used(n, false);
- cout << "Fill in the matrix of adjacency: press 1 for yes, 0 for no\n";
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- cin >> v[i][j];
- }
- }
- stack *h = init();
- stack *res = init();
- for(int i = 0; i < n; i++){
- push(h, i);
- while(h){
- int cur = top(h);
- //if(used[cur]){
- // pop(h);
- // break;
- //}
- bool flag = false;//shows true if there are no unused tops left
- int j = 0;
- while(j < n){
- if(v[cur][j] && !used[j]){
- push(h, cur);
- break;
- }
- else j++;
- }
- if(flag){
- used[cur] = true;
- pop(h);
- push(res, pop(h));
- }
- }
- }
- while(res){
- cout << pop(res) << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement