Advertisement
Tkap1

Untitled

Feb 19th, 2024
2,682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1.  
  2.  
  3. #include <vector>
  4. #include <iostream>
  5. #include <stdint.h>
  6.  
  7. using namespace std;
  8.  
  9. typedef uint32_t u32;
  10. struct s_visited
  11. {
  12.     u32 a;
  13.     u32 b;
  14.     u32 index;
  15. };
  16.  
  17. struct s_connection
  18. {
  19.     u32 a;
  20.     u32 b;
  21. };
  22. vector<s_connection> connections = {};
  23.  
  24. bool has_path(u32 a, u32 b, vector<s_visited>* visited)
  25. {
  26.     for(u32 i = 0; i < connections.size(); i++) {
  27.         s_connection c = connections[i];
  28.  
  29.         if((c.a == a && c.b == b) || (c.b == a && c.a == b)) { return true; }
  30.         bool skip = false;
  31.         for(u32 j = 0; j < visited->size(); j++) {
  32.             s_visited v = visited->at(j);
  33.             if(v.index == i && (v.a == a && v.b == b) || (v.b == a && v.a == b)) {
  34.                 skip = true;
  35.                 break;
  36.             }
  37.         }
  38.         if(skip) { continue; }
  39.         visited->push_back({a, b, i});
  40.  
  41.         if(c.a == a) {
  42.             if(has_path(c.b, b, visited)) { return true; }
  43.         }
  44.  
  45.         if(c.b == a) {
  46.             if(has_path(c.a, b, visited)) { return true; }
  47.         }
  48.     }
  49.  
  50.     return false;
  51. }
  52.  
  53. int main()
  54. {
  55.     u32 cities;
  56.     u32 roads;
  57.     cin >> cities >> roads;
  58.     for(u32 i = 0; i < roads; i++) {
  59.         u32 a;
  60.         u32 b;
  61.         cin >> a >> b;
  62.         connections.push_back({a, b});
  63.     }
  64.  
  65.     u32 curr = 1;
  66.     vector<s_connection> added = {};
  67.     for(u32 i = 0; i < cities; i++) {
  68.         u32 ii = i + 1;
  69.         if(curr == ii) { continue; }
  70.         vector<s_visited> visited = {};
  71.         if(!has_path(curr, ii, &visited)) {
  72.             connections.push_back({curr, ii});
  73.             added.push_back({curr, ii});
  74.         }
  75.         curr += 1;
  76.     }
  77.     printf("%zu\n", added.size());
  78.     for(u32 i = 0; i < added.size(); i++) {
  79.         printf("%i %i\n", added[i].a, added[i].b);
  80.     }
  81.     return 0;
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement