This commit is contained in:
Zengtudor 2024-11-20 23:11:56 +08:00
parent 1c0c534c5a
commit 8c18daa10a

96
src/P7113/P7113.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using i128 = __int128;
const ll maxn{ll(1e5+5)};
ll n,m,ind[maxn];
pair<i128,i128> nds[maxn];
vector<ll> adj[maxn];
i128 gcd128(i128 m,i128 n){
while(n!=0){
i128 t{m%n};
m=n;
n=t;
}
return m;
}
void sf(i128 &p,i128 &q){
i128 gcda{gcd128(p,q)};
p/=gcda;
q/=gcda;
}
pair<i128,i128> add(i128 p1,i128 q1,i128 p2,i128 q2){
i128 pa,qa;
i128 lcm{q1*q2/gcd128(q1,q2)};
qa=lcm;
p1=p1*(lcm/q1);
p2=p2*(lcm/q2);
pa=p1+p2;
sf(pa,qa);
return {pa,qa};
}
ostream &operator<<(ostream &os, i128 num){
if(num < 0){
os << '-';
num = -num;
}
string s;
if(num == 0){
s = "0";
}
while(num > 0){
s += '0' + (num % 10);
num /= 10;
}
reverse(s.begin(), s.end());
os << s;
return os;
}
int main(){
iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
cin>>n>>m;
for(ll i{1};i<=n;i++){
if(i<=m){
nds[i]={1,1};
}else{
nds[i]={0,1};
}
ll nn;
cin>>nn;
for(ll j{1};j<=nn;j++){
ll d;
cin>>d;
adj[i].emplace_back(d);
ind[d]++;
}
}
queue<ll> qu;
for(ll i{1};i<=n;i++){
if(ind[i]==0){
qu.emplace(i);
}
}
while(qu.size()){
auto u {qu.front()};
qu.pop();
for(auto &v:adj[u]){
nds[v]=add(nds[v].first,nds[v].second,nds[u].first,nds[u].second*adj[u].size());
if(--ind[v]==0){
qu.emplace(v);
}
}
}
for(ll i{1};i<=n;i++){
if(adj[i].size()==0){
cout<<nds[i].first<<' '<<nds[i].second<<'\n';
}
}
}