Advertisement
f1recracker

Untitled

Feb 28th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define NODE_MAX 500000
  4.  
  5. using namespace std;
  6.  
  7. typedef struct node{
  8.     int data;
  9.     struct node* next;
  10. } node;
  11.  
  12. /*Swap two integers*/
  13. void swap(int* x, int* y){
  14.     int temp = *i;
  15.     *i = *j;
  16.     *j = temp;
  17. }
  18.  
  19. /*Add node to head*/
  20. void makeNode(int i, int j){
  21.     node* newNode = (node*)malloc(sizeof(node));
  22.     newNode->next = adjList[i]->next;
  23.     newNode->data = j;
  24.     adjList->next = newNode;
  25. }
  26.  
  27. void traverse(int i){
  28.     int j = 0;
  29.     node* current = adjList[i]->next;
  30.     int nearestNeighbours[NODE_MAX] = {0};
  31.  
  32.     for (int j = 0; i < n; j++){
  33.         if (j < current->data ){
  34.             nearestNeighbours[i] = 1;
  35.             /*Print they are neighbours*/
  36.         } else if (j == current->data){
  37.             /*Road here*/
  38.             /*do bfs, and print*/ /*shouldn't take more than 2-3 iterations typically*/
  39.         } else {
  40.             j--; /*Dont update j and counter the ++ at end of for loop, increment ptr*/
  41.             if (current->next!=NULL){
  42.                 current = current->next;
  43.             } else {
  44.                 return ;
  45.             }
  46.         }
  47.     }
  48.    
  49. }
  50.  
  51. /*bubble sort for list */
  52. void sort(node** adjList){
  53.     for(int i = 0; i < n; i++){
  54.         node* current = adjList[i]->next;
  55.         node* temp = current;
  56.         node* min = temp;
  57.  
  58.         while(current->next!=NULL){
  59.             while(temp->next = null){
  60.                 if (temp->data < min->data){
  61.                     min=temp;
  62.                 }
  63.                 temp=temp->next;
  64.             }
  65.             swap( &(current->data), &(min->data) );
  66.             current=current->next;
  67.         }
  68.     }
  69. }
  70.  
  71. node** adjList;
  72. long int n, m, x, y;
  73. int t; 
  74.  
  75. int main(){
  76.     cin >> t;
  77.  
  78.     while (t--){
  79.         cin >> n;
  80.         adjList = (node**)malloc(sizeof(node*)*n);
  81.         cin >> m;
  82.  
  83.         while (m--){
  84.             cin >> x >> y;
  85.             if (x > y){
  86.                 swap (x, y);
  87.             }
  88.             makeNode(x,y);
  89.         }
  90.        
  91.         sort(adjList);
  92.  
  93.         for (int i = 0; i < n; i++){
  94.             traverse(i);
  95.         }
  96.  
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement