Advertisement
tomalikem

LinieHyp

May 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. // we're big guys now, let's use already implemeted list
  3. // you might want check this out: http://www.cplusplus.com/reference/list/list/#functions
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. // Global variables are bad, but in this case
  9. // passing them in function would soon exceed memory limit.
  10. // Trust me, I tried.
  11. int n;
  12. list<int> *graph;
  13. int curr_line;
  14. list<int> *lines;
  15. bool is_euler;
  16.  
  17.  
  18. void euler(int u)
  19. {
  20.     while (!graph[u].empty())
  21.     {
  22.         int v = graph[u].front();   // take first neighbour of u
  23.         graph[v].remove(u);
  24.         graph[u].pop_front();
  25.         euler(v);
  26.     }
  27.  
  28.     // if we're visiting our dummy city, start a new line
  29.     if (!is_euler && u == 0) {
  30.         curr_line++;
  31.     } else {
  32.         lines[curr_line].push_back(u);
  33.         // if not, just add city to current line
  34.     }
  35. }
  36.  
  37. int main() {
  38.     int Z;
  39.  
  40.     scanf("%d", &Z);
  41.  
  42.     while (Z--) {
  43.         int m, u, v;
  44.         scanf("%d %d", &n, &m);
  45.  
  46.         graph = new list<int>[n+2];
  47.         lines = new list<int>[n+2];
  48.  
  49.         for(int i=0; i<m; i++) {
  50.             scanf("%d %d", &u, &v);
  51.             graph[u].push_back(v);
  52.             graph[v].push_back(u);
  53.         }
  54.        
  55.         is_euler = true;
  56.         for (int i = 1; i <= n; i++)
  57.         {
  58.             // add dummy city to each city that has odd number of neighbours
  59.             if (graph[i].size() & 1)
  60.             {
  61.                 graph[i].push_back(0);
  62.                 graph[0].push_back(i);
  63.                 is_euler = false;
  64.             }
  65.         }
  66.  
  67.         curr_line = 0;
  68.  
  69.         if (is_euler) {
  70.             euler(1);
  71.         } else {
  72.             euler(0);
  73.         }
  74.  
  75.         // ignore empty lines
  76.         int lines_number = 0;
  77.         for (int i = 0; i <= curr_line; i++)
  78.         {
  79.             if (lines[i].size()>1)
  80.                 lines_number++;
  81.         }
  82.  
  83.         printf("%d\n", lines_number);
  84.  
  85.         for (int i = 0; i <= lines_number; i++)
  86.         {
  87.             if (lines[i].size() > 1)
  88.             {
  89.                 printf("%lu ", lines[i].size());
  90.                 for (list<int>::iterator it = lines[i].begin(); it != lines[i].end(); it++)
  91.                 {
  92.                     printf("%d ", *it);
  93.                 }
  94.                 printf("\n");
  95.             }
  96.         }
  97.  
  98.         delete[] graph;
  99.         delete[] lines;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement