Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <compare>
- #include <absl/time/time.h>
- #include <absl/time/clock.h>
- #include <sstream>
- #include <map>
- #include <regex>
- #include <absl/algorithm/container.h>
- #include <absl/strings/str_split.h>
- using namespace std;
- struct Node {
- vector<string> edges;
- bool big;
- };
- long long CountPaths(const unordered_map<string, Node>& G, const string& from, const unordered_set<string>& visited, bool smallVisitedTwice) {
- if(from == "end") return 1ll;
- if(visited.contains(from)) {
- if (!smallVisitedTwice && from != "start") {
- smallVisitedTwice = true;
- }
- else {
- return 0;
- }
- };
- unordered_set<string> v = visited;
- auto& n = G.at(from);
- if(!n.big) {
- v.insert(from);
- }
- long long ans = 0;
- for(auto& next: n.edges) {
- ans += CountPaths(G, next, v, smallVisitedTwice);
- }
- return ans;
- }
- int main() {
- auto start = absl::Now();
- int ans = 0;
- unordered_map<string, Node> G;
- for(std::string line; std::getline(std::cin, line);) {
- vector<string> v = absl::StrSplit(line, "-");
- G[v[0]].edges.push_back(v[1]);
- G[v[1]].edges.push_back(v[0]);
- G[v[0]].big = all_of(v[0].begin(), v[0].end(), [](const auto& c) {
- return c == toupper(c);
- });
- G[v[1]].big = all_of(v[1].begin(), v[1].end(), [](const auto& c) {
- return c == toupper(c);
- });
- }
- unordered_set<string> v{};
- ans = CountPaths(G, "start", v, false);
- cout << ans << endl;
- std::cout << absl::Now() - start << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment