写了250行代码,还我7个TLEqwq
#include <bits/stdc++.h>
using namespace std;
int n, m, in[500005];
bool more(string x, string y)
{
int n = x.length(), m = y.length();
if (n > m) return 1;
if (n < m) return 0;
for (int i = 0; i < n; i++)
{
if (x[i] > y[i]) return 1;
if (x[i] < y[i]) return 0;
}
return 0;
}
bool les(string x, string y)
{
int n = x.length(), m = y.length();
if (n > m) return 0;
if (n < m) return 1;
for (int i = 0; i < n; i++)
{
if (x[i] > y[i]) return 0;
if (x[i] < y[i]) return 1;
}
return 0;
}
bool amount(string x, string y)
{
int n = x.length(), m = y.length();
if (n != m) return 0;
for (int i = 0; i < n; i++)
if (x[i] != y[i]) return 0;
return 1;
}
string add(string x, string y)
{
int n = x.length(), m = y.length();
int a[20], b[20], c[20];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++)
a[n-i] = x[i]-'0';
for (int i = 0; i < m; i++)
b[m-i] = y[i]-'0';
for (int i = 1; i <= max(n, m); i++)
{
c[i] += a[i] + b[i];
c[i+1] += c[i] / 10;
c[i] %= 10;
}
string ans;
for (int i = 1; i <= max(n, m) + (bool)c[max(n,m)+1]; i++)
ans = (char)('0'+c[i]) + ans;
if (ans == "") return "0";
return ans;
}
string minu(string x, string y)
{
int n = x.length(), m = y.length(), cnt=max(n, m);
int a[20], b[20], c[20];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++)
a[n-i] = x[i]-'0';
for (int i = 0; i < m; i++)
b[m-i] = y[i]-'0';
for (int i = 1; i <= max(n, m); i++)
{
c[i] += a[i] - b[i];
c[i+1] -= (c[i] < 0);
c[i] = (c[i] + 10) % 10;
}
string ans;
while (c[cnt] == 0) cnt--;
for (int i = 1; i <= cnt; i++)
ans = (char)('0'+c[i]) + ans;
if (ans == "") return "0";
return ans;
}
string mul(string x, string y)
{
int n = x.length(), m = y.length();
int a[20], b[20], c[20];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++)
a[n-i] = x[i]-'0';
for (int i = 0; i < m; i++)
b[m-i] = y[i]-'0';
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
c[i+j-1] += a[i] * b[j];
for (int i = 1; i < n+m; i++)
{
c[i+1] += c[i] / 10;
c[i] %= 10;
}
string ans;
for (int i = 1; i < m+n + (bool)c[n+m]; i++)
ans = (char)('0'+c[i]) + ans;
if (ans == "") return "0";
return ans;
}
string half(string x)
{
int n = x.length();
int a[20], b[20];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < n; i++)
a[i] = x[i]-'0';
for (int i = 0; i < n; i++)
{
a[i+1] += a[i] % 2 ? 10 : 0;
b[i] = a[i]/2;
}
string ans;
for (int i = b[0]?0:1; i < n; i++)
ans = ans + (char)(b[i]+'0');
if (ans == "") return "0";
return ans;
}
string divide(string x, string y)
{
if (les(x, y)) return "0";
string l = "0", r = x, ans;
while (!more(l, r))
{
string mid = half(add(l, r));
if (!more(mul(mid, y),x))
{
ans = mid;
l = add(mid, "1");
}
else
r = minu(mid, "1");
}
if (ans == "") return "0";
return ans;
}
string mod(string x, string y)
{
return minu(x, mul(divide(x, y), y));
}
string gcd(string x, string y)
{
return amount(mod(x, y), "0") ? y : gcd(y, mod(x, y));
}
string lcm(string x, string y)
{
return mul(divide(x, gcd(x, y)), y);
}
struct fraction
{
string a, b;
}w[500005];
fraction f_add(fraction x, fraction y)
{
fraction ans;
ans.b = lcm(x.b, y.b);
ans.a = add(mul(divide(ans.b, x.b), x.a), mul(divide(ans.b, y.b), y.a));
string GCD = gcd(ans.a, ans.b);
ans.a = divide(ans.a, GCD);
ans.b = divide(ans.b, GCD);
return ans;
}
fraction f_mul(fraction x, fraction y)
{
return (fraction){mul(x.a, y.a), mul(x.b, y.b)};
}
vector<int> e[500005];
queue<int> q;
int main()
{
//fraction a = {"1", "2"}, b = {"1", "3"};
//cout << f_add(a, b).a << f_add(a, b).b;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
w[i].b = "1";
int s, t;
cin >> s;
for (int j = 1; j <= s; j++)
{
cin >> t;
e[i].push_back(t);
in[t]++;
}
}
for (int i = 1; i <= m; i++)
{
w[i].a = "1";
q.push(i);
}
string p[6] = {"0", "1", "2", "3", "4", "5"};
while (q.size())
{
int u = q.front();
q.pop();
if (!(int)e[u].size()) continue;
fraction k = {"1", p[(int)e[u].size()]};
k = f_mul(k, w[u]);
w[u] = {"0", "1"};
for (int &v : e[u])
{
w[v] = f_add(w[v], k);
in[v]--;
if (!in[v])
q.push(v);
}
}
for (int i = 1; i <= n; i++)
if (w[i].a!="0")
cout << w[i].a << " " << w[i].b << endl;
return 0;
}