Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<fstream>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7. struct Edge {
  8.     int vertex;
  9.     int adjacency;
  10.     Edge* next;
  11.     Edge()
  12.     {
  13.         next = NULL;
  14.     }
  15. };
  16. void creation(Edge*&head, ifstream &input) {
  17.     Edge*current = head;
  18.     input >> head->vertex >> head->adjacency;
  19.     while (!input.eof())
  20.     {
  21.         current->next = new Edge;
  22.         current = current->next;
  23.         input >> current->vertex >> current->adjacency;
  24.     }
  25. }
  26. int the_number_of_vertices(Edge*&head) {
  27.     Edge* current = head;
  28.     int max = head->vertex;
  29.     while (current)
  30.     {
  31.         if (max < current->vertex)
  32.         {
  33.             max = current->vertex;
  34.         }
  35.         current = current->next;
  36.     }
  37.     return max;
  38. }
  39. int the_number_of_pairs_of_numbers(Edge*&head) {
  40.     Edge* current = head;
  41.     int amount = 0;
  42.     while (current)
  43.     {
  44.         ++amount;
  45.         current = current->next;
  46.     }
  47.     return amount;
  48. }
  49. void amount_of_neighbourhood(Edge*head, int *array_of_adjacency) {
  50.     Edge*current = head;
  51.     while (current)
  52.     {
  53.         ++array_of_adjacency[current->vertex];
  54.         current = current->next;
  55.     }
  56. }
  57. void creaction_array_of_adjacency(Edge*head, int *array_of_adjacency, int &size_of_array, int amount_of_edges) {
  58.     Edge*current = head;
  59.     int position = size_of_array - amount_of_edges;
  60.     for (int i = 0; i < size_of_array - amount_of_edges; ++i) {
  61.         if (array_of_adjacency[i] != 0) {
  62.             array_of_adjacency[i] = position;
  63.             current = head;
  64.             while (current) {
  65.                 if (current->vertex == i) {
  66.                     array_of_adjacency[position] = current->adjacency;
  67.                     ++position;
  68.                 }
  69.                 current = current->next;
  70.             }
  71.         }
  72.     }
  73. }
  74. void print_array(int*arr, int &size_of_array) {
  75.     for (int i = 0; i < size_of_array; ++i) {
  76.         cout << setw(4) << i;
  77.     }
  78.     cout << endl;
  79.     for (int i = 0; i < size_of_array; ++i) {
  80.         cout << setw(4) << arr[i];
  81.     }
  82.     cout << endl;
  83. }
  84. void print_vertex_and_adjacency(int* arr, int &size_of_array, int &amount_of_edges) {
  85.     int PrintBorder = 0;
  86.     for (int i = 0; i < size_of_array - amount_of_edges; ++i) {
  87.         if (arr[i] != 0) {
  88.             PrintBorder = size_of_array;
  89.             int j = i + 1;
  90.             while (j < size_of_array - amount_of_edges) {
  91.                 if (arr[j] != 0) {
  92.                     PrintBorder = arr[j];
  93.                     break;
  94.                 }
  95.                 ++j;
  96.             }
  97.             cout << i << " : ";
  98.             for (int j = arr[i]; j < PrintBorder; ++j)
  99.             {
  100.                 cout << arr[j] << "  ";
  101.             }
  102.             cout << endl;
  103.         }
  104.     }
  105. }
  106. int main() {
  107.     ifstream input("input.txt");
  108.  
  109.     Edge *Edges = new Edge;
  110.     creation(Edges, input);
  111.     int amount_of_edges = the_number_of_pairs_of_numbers(Edges);
  112.     int size_of_array = the_number_of_vertices(Edges) + 1 + amount_of_edges;
  113.     int *array_of_adjacency = new int[size_of_array];
  114.     for (int i = 0; i < size_of_array; ++i)
  115.     {
  116.         array_of_adjacency[i] = 0;
  117.     }
  118.     amount_of_neighbourhood(Edges, array_of_adjacency);
  119.     creaction_array_of_adjacency(Edges, array_of_adjacency, size_of_array, amount_of_edges);
  120.     print_array(array_of_adjacency, size_of_array);
  121.     print_vertex_and_adjacency(array_of_adjacency, size_of_array, amount_of_edges);
  122.     system("pause");
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement