Advertisement
Sanlover

Untitled

Nov 23rd, 2021
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <conio.h>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <map>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. vector<size_t> usedPoints;
  9. vector<pair<size_t, size_t>> pairs;
  10.  
  11. static bool isInside(map<size_t, vector<size_t>>& map, const size_t& key) {
  12.   return map.find(key) != map.end();
  13. }
  14.  
  15. static bool isUsed(const size_t& key) {
  16.   for (auto& jt : usedPoints) {
  17.     if (jt == key) {
  18.       return true;
  19.     }
  20.   }
  21.   return false;
  22. }
  23.  
  24. static bool isFound(map<size_t, vector<size_t>>& map,
  25.                     const size_t& lookingIn,
  26.                     const size_t& lookingFor) {
  27.   usedPoints.push_back(lookingIn);
  28.   for (auto& it : map[lookingIn]) {
  29.     if (it == lookingFor) {
  30.       usedPoints.clear();
  31.       return true;
  32.     }
  33.     if (!isUsed(it)) {
  34.       return isFound(map, it, lookingFor);
  35.     }
  36.   }
  37.   usedPoints.clear();
  38.   return false;
  39. }
  40.  
  41. int main() {
  42.   size_t pairAmount, choice;
  43.   map<size_t, vector<size_t>> pointMap;
  44.   ifstream input("input.txt");
  45.   if (!input.is_open()) {
  46.     cout << "File not found.";
  47.     return -1;
  48.   }
  49.  
  50.   cout << "Select input type (1 - file; 2 - console): ";
  51.   cin >> choice;
  52.   if (choice != 1 && choice != 2) {
  53.     cout << "Wrong type, please pick 1 or 2";
  54.     return -1;
  55.   }
  56.   size_t first, second;
  57.   if (choice == 1) {
  58.     while (input.peek() != EOF) {
  59.       input >> first >> second;
  60.       if (isInside(pointMap, first) || isInside(pointMap, second)) {
  61.         if (isFound(pointMap, first, second)) {
  62.         } else {
  63.           pointMap[first].push_back(second);
  64.           pointMap[second].push_back(first);
  65.           pairs.push_back(pair<size_t, size_t>(first, second));
  66.         }
  67.       } else {
  68.         pointMap[first].push_back(second);
  69.         pointMap[second].push_back(first);
  70.         pairs.push_back(pair<size_t, size_t>(first, second));
  71.       }
  72.     }
  73.   } else {
  74.     cout << "Enter the amount of pairs you want to enter later: ";
  75.     cin >> pairAmount;
  76.  
  77.     cout << endl << "Enter pairs: " << endl;
  78.     for (size_t i = 0; i < pairAmount; i++) {
  79.       cout << i + 1 << ") ";
  80.       cin >> first >> second;
  81.       if (isInside(pointMap, first) || isInside(pointMap, second)) {
  82.         if (isFound(pointMap, first, second)) {
  83.         } else {
  84.           pointMap[first].push_back(second);
  85.           pointMap[second].push_back(first);
  86.           pairs.push_back(pair<size_t, size_t>(first, second));
  87.         }
  88.       } else {
  89.         pointMap[first].push_back(second);
  90.         pointMap[second].push_back(first);
  91.         pairs.push_back(pair<size_t, size_t>(first, second));
  92.       }
  93.     }
  94.   }
  95.   cout << endl;
  96.   for (auto& it : pairs) {
  97.     cout << it.first << "-" << it.second << " ";
  98.   }
  99.   return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement