跳到正文

个人记录

5

haobucuo333约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月17日
收录
2026年10月17日
更新
2026年11月20日
篇幅
约 3 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1
C++
#include<bits/stdc++.h>
#define i128 __int128
#define update(x,y,a,b) i128 p=y*b/gcd(y,b),x=p/y,a=p/b,x+=a,y=p;
using namespace std;
const int N=1e5+5;
i128 gcd(i128 a,i128 b){
	if(b==0)	return a;
	return gcd(b,a%b);
}
i128 read(){
	i128 x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')	f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=getchar();
	return x*f;
}
void print(i128 x){
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)	print(x/10);
	putchar(x%10+'0');
}
queue<int> q;
i128 n,m;
i128 fz[N],fm[N],chu[N];
int tot,head[N],ru[N];
struct node{
	int nxt,to;
}e[N<<1];

void add(int u,int v){
	e[++tot].nxt=head[u];
	e[tot].to=v;
	head[u]=tot;
}

int main(){
	n=read(),m=read();
	for(int i=1;i<=m;++i)
	{
		fz[i]=1;
		q.push(i);
	}
	for(int i=1,d;i<=n;++i)
	{
		cin>>d;
		fm[i]=1;
		chu[i]+=d;
		for(int j=1,x;j<=d;++j)
		{
			x=read();
			add(i,x);
			ru[x]++;
		}
	}
	while(q.size())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i;i=e[i].nxt)
		{
			int v=e[i].to;
			update(fz[v],fm[v],fz[u],fm[u]*chu[u]);
			ru[v]--;
			if(ru[v]==0)	q.push(v);
		}
	}
	for(int i=1;i<=n;++i)
	{
		if(chu[i]==0)
		{
			i128 p=gcd(fz[i],fm[i]);
			print(fz[i]/p);
			putchar(' ');
			print(fm[i]/p);
			putchar('\n');
		}
	}
	return 0;
}
原文