Advertisement
Tkap1

Untitled

Feb 19th, 2024 (edited)
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdint.h>
  3. using namespace std;
  4. typedef uint32_t u32;
  5.  
  6. struct s_connection {
  7.     u32 a;
  8.     u32 b;
  9. };
  10.  
  11. static bool walked[100000];
  12. static u32 connection_count = 0;
  13. static u32 added_count = 0;
  14. static s_connection connections[200000];
  15. static s_connection added[200000];
  16. static u32 cities, roads;
  17.  
  18. int main() {
  19.  
  20.     cin >> cities >> roads;
  21.     for(u32 i = 0; i < roads; i++) {
  22.         u32 a, b;
  23.         cin >> a >> b;
  24.         connections[connection_count++] = {a, b};
  25.     }
  26.  
  27.     walked[1] = true;
  28.     for(u32 city_i = 1; city_i < cities; city_i++) {
  29.         u32 curr = city_i;
  30.         u32 target = city_i + 1;
  31.         if(walked[target]) {
  32.             continue;
  33.         }
  34.  
  35.         bool reset_loop = true;
  36.         while(reset_loop) {
  37.             reset_loop = false;
  38.             for(int connection_i = 0; connection_i < (int)connection_count; connection_i++) {
  39.                 s_connection c = connections[connection_i];
  40.                 if(walked[c.a] && !walked[c.b]) {
  41.                     walked[c.b] = true;
  42.                     reset_loop = true;
  43.                 }
  44.                 else if(walked[c.b] && !walked[c.a]) {
  45.                     walked[c.a] = true;
  46.                     reset_loop = true;
  47.                 }
  48.                 if(walked[c.a] && walked[c.b]) {
  49.                     connections[connection_i--] = connections[--connection_count];
  50.                 }
  51.                 if(walked[target]) { break; }
  52.             }
  53.         }
  54.         if(!walked[target]) {
  55.             connections[connection_count++] = {curr, target};
  56.             added[added_count++] = {curr, target};
  57.             walked[target] = true;
  58.         }
  59.     }
  60.  
  61.     cout << added_count << endl;
  62.     for(u32 i = 0; i < added_count; i++) {
  63.         cout << added[i].a << " " << added[i].b << endl;
  64.     }
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement