Advertisement
Derga

Untitled

Sep 1st, 2023
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int64_t nodes_count, edges_count;
  9.     cin >> nodes_count >> edges_count;
  10.  
  11.     if (nodes_count == 1)
  12.     {
  13.         cout << -1 << '\n';
  14.         return 0;
  15.     }
  16.  
  17.     vector< vector<bool> > is_edge(1 + nodes_count, vector<bool>(1 + nodes_count, false));
  18.     for (int64_t i = 0; i < edges_count; ++i)
  19.     {
  20.         int64_t node1, node2;
  21.         cin >> node1 >> node2;
  22.         is_edge[node1][node2] = true;
  23.         is_edge[node2][node1] = true;
  24.     }
  25.    
  26.     vector<bool> is_adjacent_for_first_node = is_edge[1];
  27.    
  28.     vector< bool > full_subgraph_containing_first_node(1 + nodes_count, true);
  29.  
  30.     //формирую полный подграф содержащий первую вершину
  31.     for (int64_t node1 = 2; node1 < is_adjacent_for_first_node.size(); ++node1)
  32.     {
  33.         if (!is_adjacent_for_first_node[node1])
  34.         {
  35.             full_subgraph_containing_first_node[node1] = false;
  36.             continue;
  37.         }          
  38.        
  39.         for (int64_t node2 = node1 + 1; node2 <= nodes_count; ++node2)
  40.         {
  41.             if (!is_adjacent_for_first_node[node2])
  42.                 continue;
  43.  
  44.             if (!is_edge[node1][node2])
  45.                 full_subgraph_containing_first_node[node2] = false;
  46.         }
  47.     }
  48.  
  49.     vector< bool > nodes(1 + nodes_count, false);
  50.     for (int64_t i = 2; i < full_subgraph_containing_first_node.size(); ++i)
  51.     {
  52.         if (!full_subgraph_containing_first_node[i])
  53.         {
  54.             nodes[i] = true;
  55.         }
  56.     }
  57.     //теперь в nodes - все вершины, которые не вошли в полный подграф, содержащий вершину 1
  58.     //нужно проверить образуют ли эти вершины полный подграф. Если - да, то все ок, если нет - возвращаем -1.
  59.     for (int64_t node1 = 2; node1 < nodes.size(); ++node1)
  60.     {
  61.         if (!nodes[node1])
  62.             continue;
  63.  
  64.         for (int64_t node2 = node1 + 1; node2 < nodes.size(); ++node2)
  65.         {
  66.             if (!nodes[node2])
  67.                 continue;
  68.  
  69.             if (!is_edge[node1][node2])
  70.             {
  71.                 cout << -1 << '\n';
  72.                 return 0;
  73.             }
  74.         }
  75.     }
  76.  
  77.     //теперь у нас есть два полных подграфа.
  78.     int64_t sum1 = 1;
  79.     int64_t sum2 = 0;
  80.     for (int64_t i = 2; i <= nodes_count; ++i)
  81.     {
  82.         if (full_subgraph_containing_first_node[i])
  83.             sum1++;
  84.         if (nodes[i])
  85.             sum2++;
  86.     }
  87.  
  88.     if (sum2 == 0)
  89.     {
  90.         cout << nodes_count / 2 << '\n';
  91.         for (int64_t i = 2; i <= nodes_count; i += 2)
  92.         {
  93.                 cout << i << ' ';
  94.         }
  95.         cout << '\n';
  96.         for (int64_t i = 1; i <= nodes_count; i += 2)
  97.         {
  98.                 cout << i << ' ';
  99.         }
  100.     }
  101.     else
  102.     {
  103.         cout << sum1 << '\n' << 1 << ' ';
  104.         for (int64_t i = 2; i < full_subgraph_containing_first_node.size(); ++i)
  105.         {
  106.             if (full_subgraph_containing_first_node[i])
  107.             {
  108.                 cout << i << ' ';
  109.             }
  110.         }
  111.         cout << '\n';
  112.        
  113.         //cout << sum2 << '\n';
  114.         for (int64_t i = 2; i < nodes.size(); ++i)
  115.         {
  116.             if (nodes[i])
  117.             {
  118.                 cout << i << ' ';
  119.             }
  120.         }
  121.     }
  122.  
  123.     return 0;
  124. }
  125.  
  126. /*
  127. test1
  128. 3 1
  129. 1 2
  130. answer
  131. 2
  132. 1 2
  133. 3
  134.  
  135. test2
  136. 3 0
  137. answer
  138. -1
  139.  
  140. test3
  141. 5 10
  142. 1 2
  143. 1 3
  144. 1 4
  145. 1 5
  146. 2 3
  147. 2 4
  148. 2 5
  149. 3 4
  150. 3 5
  151. 4 5
  152. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement