Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct node{
  4.     int val;
  5.     node* next;
  6. };
  7. void mlist(struct node** tab,int n){ //wypisz
  8.     for(int i = 0; i < n; i++){
  9.         cout << "A[" << i << "] =";
  10.         struct node* p = tab[i];
  11.         while(p)
  12.         {
  13.           cout << "  " << p->val;
  14.           p = p->next;
  15.         }
  16.         cout << endl;
  17.     }
  18. }
  19. int dfs(node** tab,int start,bool visited[]){
  20.     visited[start] = true;
  21.     int ile = 0;
  22.     cout<<start;
  23.     struct node* p = tab[start];
  24.     int pom = 0;
  25.     if(p == NULL){
  26.         ile=ile+1;
  27.     }
  28.     while(p){
  29.         if(visited[p->val] == false) {
  30.             //cout<<endl<<"p->val"<<p->val<<"pom:"<<pom<<endl;
  31.             ile +=  dfs(tab,p->val,visited);
  32.         }
  33.         //ile = ile+pom;
  34.         p=p->next;
  35.     }
  36.     return ile+1;
  37. }
  38.  
  39. bool canGoAnywhere(int n,int m,int trains[][2]){
  40.     if(m<n-1) {
  41.         cout<<"cannot";
  42.         return false;
  43.     }
  44.     struct node** A = new node * [n];
  45.     bool visited[n] = {false};
  46.     for(int i = 0; i<n; i++) A[i] = NULL;
  47.     for(int i = 0;i<m;i++){
  48.         if(A[trains[i][0]] == NULL){
  49.             struct node* p = new node;
  50.             p->val=trains[i][1];
  51.             p->next=NULL;
  52.             A[trains[i][0]]=p;
  53.         }
  54.         else{
  55.             struct node* q = A[trains[i][0]];
  56.             while(q->next){
  57.                 q=q->next;
  58.             }
  59.             struct node* p = new node;
  60.             p->val=trains[i][1];
  61.             p->next=NULL;
  62.             q->next=p;
  63.         }
  64.         if(A[trains[i][1]]==NULL){
  65.             struct node* p = new node;
  66.             p->val=trains[i][0];
  67.             p->next=NULL;
  68.             A[trains[i][1]]=p;
  69.         }
  70.         else{
  71.             struct node* q = A[trains[i][1]];
  72.             while(q->next){
  73.                 q=q->next;
  74.             }
  75.             struct node* p = new node;
  76.             p->val=trains[i][0];
  77.             p->next=NULL;
  78.             q->next=p;
  79.         }
  80.     }
  81.     struct node* p = A[0];
  82.     mlist(A,n);
  83.     int res=dfs(A,0,visited);
  84.     if(res==n){
  85.         return true;
  86.     }
  87.     //cout<<endl<<"res: "<<dfs(A,0,visited)<<endl;
  88.     else{
  89.         return false;
  90.     }
  91.     //mlist(A,n);
  92. }
  93.  
  94. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  95.  
  96. int main(int argc, char** argv) {
  97.     int trains[][2]={{0,1},{1,2},{2,0},{3,4}};
  98.     int n = 5;
  99.     int m = 4;
  100.     //cout<<trains[1][0];
  101.     cout<<canGoAnywhere(n,m,trains);
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement