#include <iostream> #include <vector> #include <string> using lolo = long long; lolo nwd(lolo a, lolo b); lolo nww(lolo a, lolo b); struct Frac { lolo li = 0; lolo mi = 1; Frac& operator+= (Frac const& fr) { lolo mult_this = nww(mi, fr.mi) / mi; lolo mult_other = nww(mi, fr.mi) / fr.mi; this->li *= mult_this; this->li += (fr.li * mult_other); this->mi *= mult_this; return *this; } Frac operator/ (lolo n) { int dz = nwd(li, n); return {li / dz, mi * n / dz}; } }; std::ostream& operator<< (std::ostream& os, Frac const& fr) { os << fr.li << "/" << fr.mi; return os; } lolo nwd(lolo a, lolo b) { if (a < b) { std::swap(a, b); } while (b != 0) { a = a % b; std::swap(a, b); } return a; } lolo nww(lolo a, lolo b) { return a * b / nwd(a, b); } lolo solve(std::vector< std::vector< lolo > >& plats) { std::vector< Frac > fracs(plats.size(), Frac{0, 1}); fracs[0] = Frac{1, 1}; for (int src = 0; src < plats.size(); ++src) { if (plats[src].size() == 0) continue; Frac split = fracs[src] / plats[src].size(); for (auto target : plats[src]) { fracs[target] += split; } } lolo wynik = 1; for (auto f: fracs) { wynik = nww(wynik, f.mi); } return wynik; } int main() { int l_plat; std::cin >> l_plat; std::vector< std::vector< lolo > > plats(l_plat); for (int i = 0; i < l_plat; ++i) { int num; std::cin >> num; for(int k = 0; k < num; ++k) { lolo target; std::cin >> target; plats[i].push_back(target - 1); } } std::cout << solve(plats) << "\n"; }
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 74 75 76 77 78 79 80 81 | #include <iostream> #include <vector> #include <string> using lolo = long long; lolo nwd(lolo a, lolo b); lolo nww(lolo a, lolo b); struct Frac { lolo li = 0; lolo mi = 1; Frac& operator+= (Frac const& fr) { lolo mult_this = nww(mi, fr.mi) / mi; lolo mult_other = nww(mi, fr.mi) / fr.mi; this->li *= mult_this; this->li += (fr.li * mult_other); this->mi *= mult_this; return *this; } Frac operator/ (lolo n) { int dz = nwd(li, n); return {li / dz, mi * n / dz}; } }; std::ostream& operator<< (std::ostream& os, Frac const& fr) { os << fr.li << "/" << fr.mi; return os; } lolo nwd(lolo a, lolo b) { if (a < b) { std::swap(a, b); } while (b != 0) { a = a % b; std::swap(a, b); } return a; } lolo nww(lolo a, lolo b) { return a * b / nwd(a, b); } lolo solve(std::vector< std::vector< lolo > >& plats) { std::vector< Frac > fracs(plats.size(), Frac{0, 1}); fracs[0] = Frac{1, 1}; for (int src = 0; src < plats.size(); ++src) { if (plats[src].size() == 0) continue; Frac split = fracs[src] / plats[src].size(); for (auto target : plats[src]) { fracs[target] += split; } } lolo wynik = 1; for (auto f: fracs) { wynik = nww(wynik, f.mi); } return wynik; } int main() { int l_plat; std::cin >> l_plat; std::vector< std::vector< lolo > > plats(l_plat); for (int i = 0; i < l_plat; ++i) { int num; std::cin >> num; for(int k = 0; k < num; ++k) { lolo target; std::cin >> target; plats[i].push_back(target - 1); } } std::cout << solve(plats) << "\n"; } |