Advertisement
Sanlover

tmp

Nov 23rd, 2021
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. using namespace std;
  5. using custom_map_t = map<size_t, vector<size_t>>;
  6.  
  7. vector<size_t> usedPoints(0);
  8.  
  9. static bool isInside(custom_map_t& map, const size_t& key) {
  10.   return map.find(key) != map.end();
  11. }
  12.  
  13. static bool isUsed(const size_t& key) {
  14.   for (auto& jt : usedPoints) {
  15.     if (jt == key) {
  16.       return true;
  17.     }
  18.   }
  19.   return false;
  20. }
  21.  
  22. static bool isFound(custom_map_t& map,
  23.                     const size_t& lookingIn,
  24.                     const size_t& lookingFor) {
  25.   usedPoints.push_back(lookingIn);
  26.   for (auto& it : map[lookingIn]) {
  27.     if (it == lookingFor) {
  28.       usedPoints.clear();
  29.       return true;
  30.     }
  31.     if (!isUsed(it)) {
  32.       return isFound(map, it, lookingFor);
  33.     }
  34.   }
  35.   usedPoints.clear();
  36.   return false;
  37. }
  38. int main() {
  39.   size_t pairAmount;
  40.   custom_map_t pointMap;
  41.  
  42.   cout << "Enter the amount of pairs you want to enter later: ";
  43.   cin >> pairAmount;
  44.  
  45.   cout << endl << "Enter pairs: " << endl;
  46.   for (size_t i = 0, first, second; i < pairAmount; i++) {
  47.     cout << i + 1 << ") ";
  48.     cin >> first >> second;
  49.     if (isInside(pointMap, first) || isInside(pointMap, second)) {
  50.       if (isFound(pointMap, first, second)) {
  51.         cout << "Already in" << endl;
  52.       } else {
  53.         cout << "Not found" << endl;
  54.         pointMap[first].push_back(second);
  55.         pointMap[second].push_back(first);
  56.       }
  57.     } else {
  58.       cout << "First time see" << endl;
  59.       pointMap[first].push_back(second);
  60.       pointMap[second].push_back(first);
  61.     }
  62.   }
  63.   return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement