本题中,污水应该保证先从接收口 流出,在全部流动完毕后,选择已经接收完污水的排水节点再继续流动。这符合搜索中「扩散」的含义。
【样例 】
根据题意建图:
先以 为起始点,向外部拓展:
进一步地:
然而这个样例中,起始点仅有 个。在多个接收口的情况下,我们可以分别进行考虑。
这道题的一个需要考虑的方面是搜索方式。
如果按照传统的方式,那么每一个点都会把污水流到与其相连的点。为了防止污水环流,我们需要确定搜索顺序。
这时我们引入数组 表示第 个节点的出度, 表示第 个节点的入度。
特别地,对于所有的最终排水口,都有 。
假如 的入度不为 ,则必定形成环,因而 。因此 节点的入度均为 。
不妨用链式前向星维护图中的边(即排水系统的管道),用队列维护每个待拓展的节点(即排水系统中待开始流动的节点)。搜索时,每一次取队头元素 ,并扫描与其相连的节点 。然后让点 的污水量增加并让 减少 。最后,输出出度为 的节点即可。
每个节点的污水量应当按题目要求用分数表示,这里用 分别表示每个节点污水量的分子和分母。
为了在约分时防止报错,我们初始化所有的 为 ,并把 的 值设为 。
计算 时,可令 。这样,。
本题的分母的上限可达到 ,所以会爆掉 ll 和 ull。
在 在非 NOI 环境 中,我们可以使用 __int128_t 来表示 区间内的数。而__uint128_t 可表示 区间内的数。考场上还得写高精度。
完整代码:
#include<bits/stdc++.h>
#define I128 __int128_t
using namespace std;
int n,m,cnt,head[100001],oud[100001],ind[100001];
I128 wx[100001],wy[100001];
queue<int>q; // 保存即将拓展的节点
struct node
{
int nxt,to;
}e[500001];
I128 gcd(I128 x,I128 y) // gcd 用来约分和实现加减
{
I128 t;
while(y)
{
t=y;
y=x%y;
x=t;
}
return x;
}
void add(int u,int v)
{
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void update(I128 &x_1,I128 &y_1,I128 x_2,I128 y_2) // 分数加减运算
{
I128 p=y_1*y_2/gcd(y_1,y_2);
x_1*=p/y_1;
x_2*=p/y_2;
x_1+=x_2;
y_1=p;
}
template<typename T>void read(T &x)
{
x=0;
char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
}
void write(I128 x) // __int128_t 需要快读快写
{
if(x>9)write(x/10);
putchar(x%10+'0');
}
int main()
{
read(n),read(m);
for(register int i=1;i<=m;++i)
{
wx[i]=1;
q.push(i);
}
for(register int i=1,d;i<=n;++i)
{
read(d);
wy[i]=1; // 将分母都标记为 1,防止出错
oud[i]+=d;
for(register int j=1,x;j<=d;++j)
{
read(x);
add(i,x); // 增加一条从 i 到 x 的边
ind[x]++; // x 的入度加 1
}
}
while(q.size())
{
int u=q.front();
q.pop();
for(int i=head[u],v;i;i=e[i].nxt)
{
v=e[i].to;
update(wx[v],wy[v],wx[u],oud[u]*wy[u]);
ind[v]--; // 将入度减少 1
if(!ind[v])q.push(v);
}
}
for(register int i=1;i<=n;++i)
{
if(!oud[i])
{
I128 p=gcd(wx[i],wy[i]); // 约分操作
write(wx[i]/p);
putchar(' ');
write(wy[i]/p);
putchar('\n');
}
}
return 0;
}本题还可用深搜解决:
#include<stdio.h>
#define I128 __int128_t
int n,m,cnt,head[100001],oud[100001],ind[100001];
I128 wx[100001],wy[100001];
struct node
{
int nxt,to;
}e[500001];
I128 gcd(I128 x,I128 y)
{
I128 t;
while(y)
{
t=y;
y=x%y;
x=t;
}
return x;
}
void add(int u,int v)
{
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void update(I128 &x_1,I128 &y_1,I128 x_2,I128 y_2)
{
I128 p=y_1*y_2/gcd(y_1,y_2);
x_1*=p/y_1;
x_2*=p/y_2;
x_1+=x_2;
y_1=p;
}
void dfs(int u)
{
for(int i=head[u],v;i;i=e[i].nxt)
{
v=e[i].to;
update(wx[v],wy[v],wx[u],oud[u]*wy[u]);
ind[v]--;
if(!ind[v])dfs(v); // 与上面类似,递归即可
}
}
template<typename T>void read(T &x)
{
x=0;
char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
}
void write(I128 x)
{
if(x>9)write(x/10);
putchar(x%10+'0');
}
int main()
{
read(n),read(m);
for(register int i=1;i<=m;++i)wx[i]=1;
for(register int i=1,d;i<=n;++i)
{
read(d);
wy[i]=1;
oud[i]+=d;
for(register int j=1,x;j<=d;++j)
{
read(x);
add(i,x);
ind[x]++;
}
}
for(register int i=1;i<=m;++i)dfs(i);
// 按顺序分别让 1 到 m 号接收口的污水开始流动
for(register int i=1;i<=n;++i)
{
if(!oud[i])
{
I128 p=gcd(wx[i],wy[i]);
write(wx[i]/p);
putchar(' ');
write(wy[i]/p);
putchar('\n');
}
}
return 0;
}

