#include <iostream> #include <vector> long long getGCD(long long a, long long b) { if (a < b) { std::swap(a, b); } while (b > 0) { a %= b; std::swap(a, b); } return a; } long long getLCM(long long a, long long b) { return a / getGCD(a , b) * b; } struct Platform { std::vector<long> parents; std::vector<long> children; long long resetCount{1}; }; struct Solution { void setParents() { for (long i = 0; i < platforms.size(); i++) { for (long j = 0; j < platforms[i].children.size(); j++) { platforms[platforms[i].children[j]].parents.push_back(i); } } } void calculateResetCountForPlatform(long platformIndex) { Platform ¤tPlatform = platforms[platformIndex]; if (currentPlatform.children.empty()) { return; } for (long i = 0; i < currentPlatform.parents.size(); i++) { currentPlatform.resetCount = getLCM(currentPlatform.resetCount, platforms[currentPlatform.parents[i]].resetCount * currentPlatform.children.size()); } } long long calculateResetCount() { setParents(); long long totalResetCount{1}; for (long i = 0; i < platforms.size(); i++) { calculateResetCountForPlatform(i); totalResetCount = getLCM(totalResetCount, platforms[i].resetCount); } return totalResetCount; } std::vector<Platform> platforms; }; int main() { long n{}; std::cin >> n; Solution solution{}; for (long i = 0; i < n; ++i) { long k{}; std::cin >> k; Platform newPlatform{}; for (long j = 0; j < k; j++) { long tmp{}; std::cin >> tmp; newPlatform.children.push_back(tmp - 1); } solution.platforms.push_back(newPlatform); } std::cout << solution.calculateResetCount() << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <iostream> #include <vector> long long getGCD(long long a, long long b) { if (a < b) { std::swap(a, b); } while (b > 0) { a %= b; std::swap(a, b); } return a; } long long getLCM(long long a, long long b) { return a / getGCD(a , b) * b; } struct Platform { std::vector<long> parents; std::vector<long> children; long long resetCount{1}; }; struct Solution { void setParents() { for (long i = 0; i < platforms.size(); i++) { for (long j = 0; j < platforms[i].children.size(); j++) { platforms[platforms[i].children[j]].parents.push_back(i); } } } void calculateResetCountForPlatform(long platformIndex) { Platform ¤tPlatform = platforms[platformIndex]; if (currentPlatform.children.empty()) { return; } for (long i = 0; i < currentPlatform.parents.size(); i++) { currentPlatform.resetCount = getLCM(currentPlatform.resetCount, platforms[currentPlatform.parents[i]].resetCount * currentPlatform.children.size()); } } long long calculateResetCount() { setParents(); long long totalResetCount{1}; for (long i = 0; i < platforms.size(); i++) { calculateResetCountForPlatform(i); totalResetCount = getLCM(totalResetCount, platforms[i].resetCount); } return totalResetCount; } std::vector<Platform> platforms; }; int main() { long n{}; std::cin >> n; Solution solution{}; for (long i = 0; i < n; ++i) { long k{}; std::cin >> k; Platform newPlatform{}; for (long j = 0; j < k; j++) { long tmp{}; std::cin >> tmp; newPlatform.children.push_back(tmp - 1); } solution.platforms.push_back(newPlatform); } std::cout << solution.calculateResetCount() << std::endl; return 0; } |