C++#include <vector>
#include <algorithm>
#include <string>
class NaturalNumber {
public:
std::vector<unsigned int> nums;
static const long long intmax = 4294967296ll;
NaturalNumber(std::vector<unsigned long long>& x) {
nums.clear();
for (size_t i = 0; i < x.size(); ++i) {
nums.push_back(x[i]);
}
}
template <typename T>
static void dropTail(T& x) {
while (x.size() > 0 && x[x.size() - 1] == 0) {
x.pop_back();
}
}
static bool compare(NaturalNumber& a, NaturalNumber& b, bool (*compFunc_True)(unsigned a, unsigned b), bool (*compFunc_False)(unsigned a, unsigned b), bool defaultValue) {
dropTail(a.nums);
dropTail(b.nums);
if (a.nums.size() == b.nums.size()) {
for (int i = a.nums.size() - 1; i >= 0; -- i) {
if (compFunc_True(a.nums[i], b.nums[i])) {
return true;
}
if(compFunc_False(a.nums[i], b.nums[i])) {
return false;
}
}
return defaultValue;
}
else {
return compFunc_True(a.nums.size(), b.nums.size());
}
}
// returns the rest and applys the operation directly on a
static int decimal_divide(std::string& a, long long k) {
std::string res;
long long rest = 0;
for (int i = a.size() - 1; i >= 0; --i) {
rest = rest * 10 + a[i];
res.push_back(rest / k);
rest %= k;
}
int resBegin = 0;
while ((unsigned int)resBegin < res.size() && res[resBegin] == 0)
++resBegin;
a.clear();
for (int i = res.size() - 1; i >= resBegin; --i)
a.push_back(res[i]);
return rest;
}
template<typename T>
static T add(T x, T y, long long base) {
std::vector<unsigned long long> res;
T ret;
while (x.size() < y.size())
x.push_back(0);
while (x.size() > y.size())
y.push_back(0);
for (size_t i = 0; i < x.size(); ++i)
res.push_back(static_cast<unsigned long long>(x[i]) + y[i]);
res.push_back(0);
for (size_t i = 0; i < res.size() - 1; ++i) {
res[i + 1] += res[i] / base;
res[i] %= base;
}
dropTail(res);
for (size_t i = 0; i < res.size(); ++i)
ret.push_back(res[i]);
return ret;
}
static std::string multiply(const std::string& x, long long y) {
std::vector<long long> res;
std::string ret;
res.resize(x.size() + 10 /*i.e. y.size()*/ + 1);
for (size_t i = 0; i < x.size(); ++i)
res[i] = x[i] * y;
for (size_t i = 0; i < res.size() - 2; ++i) {
res[i + 1] += res[i] / 10;
res[i] %= 10;
}
while (res.size() > 0 && res[res.size() - 1] == 0)
res.pop_back();
for (size_t i = 0; i < res.size(); ++i)
ret.push_back(res[i]);
return ret;
}
static std::string split_into_chars(unsigned int x) {
std::string ret;
while (x > 0) {
ret.push_back(x % 10);
x /= 10;
}
return ret;
}
void leftShift(int digits) {
int temp = nums.size();
nums.resize(nums.size() + digits);
for(int i = temp - 1; i >= 0; -- i) {
nums[i + digits] = nums[i];
nums[i] = 0;
}
}
NaturalNumber() {
nums.clear();
}
NaturalNumber operator = (unsigned long long x) {
while (x > 0) {
nums.push_back(x % intmax);
x /= intmax;
}
return *this;
}
NaturalNumber(unsigned long long x) {
*this = x;
}
NaturalNumber(const NaturalNumber &x) {
nums = x.nums;
}
NaturalNumber operator = (NaturalNumber x) {
nums = x.nums;
return *this;
}
NaturalNumber operator = (std::string decimal) {
std::string input = "";
for (int i = decimal.size() - 1; i >= 0; --i)
input.push_back(decimal[i] - '0');
while (input.size() > 0 && input[input.size() - 1] == 0)
input.pop_back();
nums.clear();
while (input.size() > 0)
nums.push_back(decimal_divide(input, intmax));
dropTail(nums);
return *this;
}
std::string to_string() {
if (nums.size() == 0)
return "0";
std::string ret;
std::string debug;
for (int i = nums.size() - 1; i >= 0; --i) {
ret = multiply(ret, intmax);
std::string converted = split_into_chars(nums[i]);
ret = add(ret, converted, 10);
}
for (unsigned int i = 0; i < ret.size(); ++i)
ret[i] += '0';
reverse(ret.begin(), ret.end());
return ret;
}
friend std::istream& operator >> (std::istream& in, NaturalNumber& x) {
std::string inputstr;
in >> inputstr;
x = inputstr;
return in;
}
friend std::ostream& operator << (std::ostream& out, NaturalNumber x) {
std::string res = x.to_string();
out << res;
return out;
}
bool operator == (NaturalNumber x) {
return compare(*this, x,
[](unsigned, unsigned) { return false; },
[](unsigned a, unsigned b) { return a != b; }, true);
}
bool operator < (NaturalNumber x) {
return compare(*this, x,
[](unsigned a, unsigned b) { return a < b; },
[](unsigned a, unsigned b) { return a > b; }, false);
}
bool operator <= (NaturalNumber x) {
return compare(*this, x,
[](unsigned a, unsigned b) { return a < b; },
[](unsigned a, unsigned b) { return a > b; }, true);
}
bool operator > (NaturalNumber x) {
return compare(*this, x,
[](unsigned a, unsigned b) { return a > b; },
[](unsigned a, unsigned b) { return a < b; }, false);
}
bool operator >= (NaturalNumber x) {
return compare(*this, x,
[](unsigned a, unsigned b) { return a < b; },
[](unsigned a, unsigned b) { return a > b; }, true);
}
NaturalNumber operator + (NaturalNumber x) {
NaturalNumber ret;
ret.nums = add(nums, x.nums, intmax);
return ret;
}
NaturalNumber operator += (NaturalNumber x) {
return *this = *this + x;
}
NaturalNumber operator ++ () {
NaturalNumber one;
one.nums.push_back(1);
*this = *this + one;
return *this;
}
NaturalNumber operator ++ (int) {
NaturalNumber temp = *this;
++ *this;
return temp;
}
static NaturalNumber difference(NaturalNumber a, NaturalNumber b) {
if(a < b) {
std::swap(a, b);
}
long digits = a.nums.size() < b.nums.size() ? a.nums.size() : b.nums.size();
for(int i = digits - 1; i >= 0; -- i) {
if(a.nums[i] < b.nums[i]) {
-- a.nums[i + 1];
}
a.nums[i] += intmax - b.nums[i];
}
dropTail(a.nums);
return a;
}
NaturalNumber operator * (NaturalNumber x) {
if (nums.size() == 0 || x.nums.size() == 0) {
return NaturalNumber();
}
std::vector<unsigned long long> res;
res.resize(nums.size() + x.nums.size());
for (size_t i = 0; i < nums.size(); ++i) {
for (size_t j = 0; j < x.nums.size(); ++j)
res[i + j] += (unsigned long long) nums[i] * x.nums[j];
for (size_t j = 0; j < res.size() - 1; ++j) {
res[j + 1] += res[j] / intmax;
res[j] %= intmax;
}
}
while (res[res.size() - 1] == 0)
res.pop_back();
return NaturalNumber(res);
}
NaturalNumber operator *= (NaturalNumber x) {
return *this = *this * x;
}
NaturalNumber operator / (NaturalNumber x) {
NaturalNumber remainder;
std::vector<unsigned long long> res(nums.size());
for(int i = nums.size() - 1; i >= 0; -- i) {
remainder.leftShift(1);
remainder.nums[0] = nums[i];
NaturalNumber temp;
long long l = 0, r = intmax;
while(l < r) {
long long mid = (l + r) / 2;
if(x * mid <= remainder) {
l = mid + 1;
} else {
r = mid;
}
}
res[i] = l - 1;
remainder = difference(remainder, x * (l - 1));
}
return NaturalNumber(res);
}
friend NaturalNumber operator + (unsigned long long x, NaturalNumber y) {
return y + x;
}
friend NaturalNumber operator * (unsigned long long x, NaturalNumber y) {
return y * x;
}
friend bool operator == (unsigned long long x, NaturalNumber y) {
return y == (NaturalNumber)x;
}
friend bool operator < (unsigned long long x, NaturalNumber y) {
return y > x;
}
friend bool operator <= (unsigned long long x, NaturalNumber y) {
return y >= x;
}
friend bool operator > (unsigned long long x, NaturalNumber y) {
return y < x;
}
friend bool operator >= (unsigned long long x, NaturalNumber y) {
return y <= x;
}
};
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 1e5;
int n, m;
vector<int> graph[MAXN + 5];
class Rational {
public:
__int128 p, q;
bool simplified;
Rational() {
this->p = 0;
this->q = 1;
this->simplified = true;
}
Rational(__int128 p, __int128 q) {
this->p = p;
this->q = q;
this->simplified = false;
}
Rational(const Rational& other) {
this->p = other.p;
this->q = other.q;
this->simplified = other.simplified;
}
Rational operator+(Rational &other) {
Rational ans = Rational(this->p * other.q + this->q * other.p, this->q * other.q);
ans.simplify();
return ans;
}
Rational operator=(Rational other) {
this->p = other.p;
this->q = other.q;
this->simplified = other.simplified;
return *this;
}
void simplify() {
__int128 gcd = __gcd(p, q);
p /= gcd;
q /= gcd;
}
};
Rational current[MAXN + 5], ans[MAXN + 5];
bool isExit[MAXN + 5];
int indeg[MAXN + 5];
vector<int> topres;
void topsort() {
queue<int> q;
for(int i = 1; i <= m; ++i)
q.push(i);
for(int i = 1; i <= n; ++i)
for(int u : graph[i])
++indeg[u];
while(!q.empty()) {
int u = q.front();
q.pop();
topres.push_back(u);
for(int v : graph[u]) {
--indeg[v];
if(indeg[v] == 0)
q.push(v);
}
}
}
void output(__int128 x) {
if(x == 0) {
cout << "0";
return;
}
string s = "";
while(x > 0) {
s.push_back(x % 10 + '0');
x /= 10;
}
reverse(s.begin(), s.end());
cout << s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen(R"(E:\2019-2024年联赛题目和数据\2020\2020NOIP\water7.in)", "r", stdin);
// freopen(R"(E:\2019-2024年联赛题目和数据\2020\out.out)", "w", stdout);
// freopen(R"(D:\360极速浏览器X下载\water\water\water3.in)", "r", stdin);
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
int cnt;
cin >> cnt;
if(cnt == 0)
isExit[i] = true;
for(int j = 0; j < cnt; ++j) {
int to;
cin >> to;
graph[i].push_back(to);
}
}
topsort();
for(int i = 1; i <= m; ++i) {
current[i] = Rational(1, 1);
}
for(int i : topres) {
if(current[i].p == 0)
continue;
Rational deltaCur = Rational(current[i].p, current[i].q * graph[i].size());
for(int u : graph[i]) {
if(isExit[u])
ans[u] = ans[u] + deltaCur;
else
current[u] = current[u] + deltaCur;
}
}
for(int i = 1; i <= n; ++i) {
if(isExit[i]) {
ans[i].simplify();
output(ans[i].p);
cout << ' ';
output(ans[i].q);
cout << endl;
}
}
}