Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define NODE_MAX 500000
- using namespace std;
- typedef struct node{
- int data;
- struct node* next;
- } node;
- /*Swap two integers*/
- void swap(int* x, int* y){
- int temp = *i;
- *i = *j;
- *j = temp;
- }
- /*Add node to head*/
- void makeNode(int i, int j){
- node* newNode = (node*)malloc(sizeof(node));
- newNode->next = adjList[i]->next;
- newNode->data = j;
- adjList->next = newNode;
- }
- void traverse(int i){
- int j = 0;
- node* current = adjList[i]->next;
- int nearestNeighbours[NODE_MAX] = {0};
- for (int j = 0; i < n; j++){
- if (j < current->data ){
- nearestNeighbours[i] = 1;
- /*Print they are neighbours*/
- } else if (j == current->data){
- /*Road here*/
- /*do bfs, and print*/ /*shouldn't take more than 2-3 iterations typically*/
- } else {
- j--; /*Dont update j and counter the ++ at end of for loop, increment ptr*/
- if (current->next!=NULL){
- current = current->next;
- } else {
- return ;
- }
- }
- }
- }
- /*bubble sort for list */
- void sort(node** adjList){
- for(int i = 0; i < n; i++){
- node* current = adjList[i]->next;
- node* temp = current;
- node* min = temp;
- while(current->next!=NULL){
- while(temp->next = null){
- if (temp->data < min->data){
- min=temp;
- }
- temp=temp->next;
- }
- swap( &(current->data), &(min->data) );
- current=current->next;
- }
- }
- }
- node** adjList;
- long int n, m, x, y;
- int t;
- int main(){
- cin >> t;
- while (t--){
- cin >> n;
- adjList = (node**)malloc(sizeof(node*)*n);
- cin >> m;
- while (m--){
- cin >> x >> y;
- if (x > y){
- swap (x, y);
- }
- makeNode(x,y);
- }
- sort(adjList);
- for (int i = 0; i < n; i++){
- traverse(i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement