Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <fstream>
- #include <iostream>
- #include <map>
- #include <vector>
- using namespace std;
- vector<size_t> usedPoints;
- vector<pair<size_t, size_t>> pairs;
- static bool isInside(map<size_t, vector<size_t>>& map, const size_t& key) {
- return map.find(key) != map.end();
- }
- static bool isUsed(const size_t& key) {
- for (auto& jt : usedPoints) {
- if (jt == key) {
- return true;
- }
- }
- return false;
- }
- static bool isFound(map<size_t, vector<size_t>>& map,
- const size_t& lookingIn,
- const size_t& lookingFor) {
- usedPoints.push_back(lookingIn);
- for (auto& it : map[lookingIn]) {
- if (it == lookingFor) {
- usedPoints.clear();
- return true;
- }
- if (!isUsed(it)) {
- return isFound(map, it, lookingFor);
- }
- }
- usedPoints.clear();
- return false;
- }
- int main() {
- size_t pairAmount, choice;
- map<size_t, vector<size_t>> pointMap;
- ifstream input("input.txt");
- if (!input.is_open()) {
- cout << "File not found.";
- return -1;
- }
- cout << "Select input type (1 - file; 2 - console): ";
- cin >> choice;
- if (choice != 1 && choice != 2) {
- cout << "Wrong type, please pick 1 or 2";
- return -1;
- }
- size_t first, second;
- if (choice == 1) {
- while (input.peek() != EOF) {
- input >> first >> second;
- if (isInside(pointMap, first) || isInside(pointMap, second)) {
- if (isFound(pointMap, first, second)) {
- } else {
- pointMap[first].push_back(second);
- pointMap[second].push_back(first);
- pairs.push_back(pair<size_t, size_t>(first, second));
- }
- } else {
- pointMap[first].push_back(second);
- pointMap[second].push_back(first);
- pairs.push_back(pair<size_t, size_t>(first, second));
- }
- }
- } else {
- cout << "Enter the amount of pairs you want to enter later: ";
- cin >> pairAmount;
- cout << endl << "Enter pairs: " << endl;
- for (size_t i = 0; i < pairAmount; i++) {
- cout << i + 1 << ") ";
- cin >> first >> second;
- if (isInside(pointMap, first) || isInside(pointMap, second)) {
- if (isFound(pointMap, first, second)) {
- } else {
- pointMap[first].push_back(second);
- pointMap[second].push_back(first);
- pairs.push_back(pair<size_t, size_t>(first, second));
- }
- } else {
- pointMap[first].push_back(second);
- pointMap[second].push_back(first);
- pairs.push_back(pair<size_t, size_t>(first, second));
- }
- }
- }
- cout << endl;
- for (auto& it : pairs) {
- cout << it.first << "-" << it.second << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement