ErnestGalbrun

Untitled

Dec 12th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <compare>
  5. #include <absl/time/time.h>
  6. #include <absl/time/clock.h>
  7. #include <sstream>
  8. #include <map>
  9. #include <regex>
  10. #include <absl/algorithm/container.h>
  11. #include <absl/strings/str_split.h>
  12.  
  13. using namespace std;
  14.  
  15. struct Node {
  16.     vector<string> edges;
  17.     bool big;
  18. };
  19.  
  20. long long CountPaths(const unordered_map<string, Node>& G, const string& from, const unordered_set<string>& visited, bool smallVisitedTwice) {
  21.     if(from == "end") return 1ll;
  22.     if(visited.contains(from)) {
  23.         if (!smallVisitedTwice && from != "start") {
  24.             smallVisitedTwice = true;
  25.         }
  26.         else {
  27.             return 0;
  28.         }
  29.     };
  30.     unordered_set<string> v = visited;
  31.     auto& n = G.at(from);
  32.     if(!n.big) {
  33.         v.insert(from);
  34.     }
  35.     long long ans = 0;
  36.     for(auto& next: n.edges) {
  37.         ans += CountPaths(G, next, v, smallVisitedTwice);
  38.     }
  39.     return ans;
  40. }
  41.  
  42. int main() {
  43.  
  44.     auto start = absl::Now();
  45.  
  46.     int ans = 0;
  47.     unordered_map<string, Node> G;
  48.     for(std::string line; std::getline(std::cin, line);) {
  49.         vector<string> v = absl::StrSplit(line, "-");
  50.         G[v[0]].edges.push_back(v[1]);
  51.         G[v[1]].edges.push_back(v[0]);
  52.         G[v[0]].big = all_of(v[0].begin(), v[0].end(), [](const auto& c) {
  53.             return c == toupper(c);
  54.         });
  55.         G[v[1]].big = all_of(v[1].begin(), v[1].end(), [](const auto& c) {
  56.             return c == toupper(c);
  57.         });
  58.     }
  59.  
  60.     unordered_set<string> v{};
  61.     ans = CountPaths(G, "start", v, false);
  62.  
  63.  
  64.     cout << ans << endl;
  65.     std::cout << absl::Now() - start << std::endl;
  66.     return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment