Skip to content
本博客自 2023 年 4 月 4 日起转为归档状态,可能不再发表新的博文。点此了解博主的竞赛生涯
This blog has been archived by the owner since April 4, 2023. It may no longer have new updates.

「NOIP2020」排水系统

检测到 KaTeX 加载失败,可能会导致文中的数学公式无法正常渲染。

#题面

#题目描述

对于一个城市来说,排水系统是极其重要的一个部分。

有一天,小 C 拿到了某座城市排水系统的设计图。排水系统由 nn 个排水结点(它们从 1n1 \sim n 编号)和若干个单向排水管道构成。每一个排水结点有若干个管道用于汇集其他排水结点的污水(简称为该结点的汇集管道),也有若干个管道向其他的排水结点排出污水(简称为该结点的排出管道)。

排水系统的结点中有 mm 个污水接收口,它们的编号分别为 1,2,,m1, 2, \ldots , m,污水只能从这些接收口流入排水系统,并且这些结点没有汇集管道。排水系统中还有若干个最终排水口,它们将污水运送到污水处理厂,没有排出管道的结点便可视为一个最终排水口。

现在各个污水接收口分别都接收了 11 吨污水,污水进入每个结点后,会均等地从当前结点的每一个排出管道流向其他排水结点,而最终排水口将把污水排出系统。

现在小 C 想知道,在该城市的排水系统中,每个最终排水口会排出多少污水。该城市的排水系统设计科学,管道不会形成回路,即不会发生污水形成环流的情况。

#输入格式

第一个两个用单个空格分隔的整数 n,mn, m。分别表示排水结点数与接收口数量。

接下来 nn 行,第 ii 行用于描述结点 ii 的所有排出管道。其中每行第一个整数 did_i 表示其排出管道的数量,接下来 did_i 个用单个空格分隔的整数 a1,a2,,adia_1, a_2, \ldots , a_{d_i} 依次表示管道的目标排水结点。

保证不会出现两条起始结点与目标结点均相同的管道。

#输出格式

输出若干行,按照编号从小到大的顺序,给出每个最终排水口排出的污水体积。其中体积使用分数形式进行输出,即每行输出两个用单个空格分隔的整数 ppqq,表示排出的污水体积为 pq\frac{p}{q}。要求 ppqq 互素,q=1q = 1 时也需要输出 qq

#输入输出样例

样例输入 #1

5 1
3 2 3 5
2 4 5
2 5 4
0
0

样例输出 #1

1 3
2 3

样例解释 #1

11 号结点是接收口,4,54, 5 号结点没有排出管道,因此是最终排水口。 11 吨污水流入 11 号结点后,均等地流向 2,3,52, 3, 5 号结点,三个结点各流入 13\frac{1}{3} 吨污水。 22 号结点流入的 13\frac{1}{3} 吨污水将均等地流向 4,54, 5 号结点,两结点各流入 16\frac{1}{6} 吨污水。 33 号结点流入的 13\frac{1}{3} 吨污水将均等地流向 4,54, 5 号结点,两结点各流入 16\frac{1}{6} 吨污水。 最终,44 号结点排出 16+16=13\frac{1}{6} + \frac{1}{6} = \frac{1}{3} 吨污水,55 号结点排出 13+16+16=23\frac{1}{3} + \frac{1}{6} + \frac{1}{6} = \frac{2}{3} 吨污水。

#数据范围与约定

测试点编号 nn \le mm \le
131 \sim 3 1010 11
464 \sim 6 103{10}^3
787 \sim 8 105{10}^5
9109 \sim 10 1010

对于全部的测试点,保证 1n1051 \le n \le {10}^51m101 \le m \le 100di50 \le d_i \le 5

数据保证,污水在从一个接收口流向一个最终排水口的过程中,不会经过超过 1010 个中间排水结点(即接收口和最终排水口不算在内)。

#思路

1m1 \sim m 节点开始向下搜索,如果搜到没有排水口的节点就更新节点的 ansans

▲ 样例 1 的搜索过程

需要注意的分子和分母需要单独存,不能使用浮点型存储,否则会有精度问题。

在均分的时候只需要将分母除以节点数即可,如下方所示:

23÷4=23×14=23×4=212=16\begin{aligned} &\frac{2}{3} \div 4 \\ =& \frac{2}{3} \times \frac{1}{4} \\ =& \frac{2}{3 \times 4}\\ =& \frac{2}{12} =& \frac{1}{6} \end{aligned}

在相加的时候可以这样做:

23+34=2×43×4+3×33×4=2×4+3×43×4=2012=53\begin{aligned} &\frac{2}{3} + \frac{3}{4} \\ =& \frac{2 \times 4}{3 \times 4} + \frac{3\times 3}{3 \times 4}\\ =& \frac{2 \times 4 + 3 \times 4}{3\times 4}\\ =& \frac{20}{12}\\ =& \frac{5}{3} \end{aligned}

赛场 AC 用时:1 小时。

别忘了开 long long

#代码

OI 题库 100 分,洛谷 90 分,官方数据 80 分。

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>

using namespace std;

int n, m, d, t;
vector<int> u[100005];
pair<long long, long long> ans[100005];

long long gcd(long long a, long long b) {
if(b == 0) return a;
return gcd(b, a%b);
}

void dfs(int x, long long first, long long second) {
if(u[x].empty()) {
if(ans[x].first && ans[x].second) {
if(ans[x].second == second) {
ans[x].first += first;
long long tmp = gcd(ans[x].first, ans[x].second);
ans[x].first /= tmp;
ans[x].second /= tmp;
}
else {
first *= ans[x].second;
ans[x].first *= second;
ans[x].first += first;
ans[x].second *= second;
long long tmp = gcd(ans[x].first, ans[x].second);
ans[x].first /= tmp;
ans[x].second /= tmp;
}
}
else {
ans[x].first = first;
ans[x].second = second;
}
return;
}
int s = u[x].size();
second *= s;
for(int i = 0 ; i < s; i++) {
dfs(u[x][i], first, second);
}
}

int main() {
cin >> n >> m;
for(int i = 1 ; i <= n ; i++) {
cin >> d;
for(int j = 1 ; j <= d ; j++) {
cin >> t;
u[i].push_back(t);
}
}
for(int i = 1 ; i <= m ; i++) {
dfs(i, 1, 1);
}
for(int i = 1 ; i <= n ; i++) {
if(u[i].empty()) {
cout << ans[i].first << ' ' << ans[i].second << endl;
}
}
return 0;
}

考后第二天就用 __int128 水过了这道题(懒得打高精)。

使用流输出 __int128 的时候还糊了个输出函数(因为没内置)。

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>

using namespace std;

int n, m, d, t;
vector<int> u[100005];
pair<__int128, __int128> ans[100005];

ostream& operator<<(ostream &__ostream, __int128 __n) {
string __o;
while (__n) {
__o.push_back(__n%10+'0');
__n /= 10;
}
reverse(__o.begin(), __o.end());
return __ostream << __o;
}

__int128 gcd(__int128 a, __int128 b) {
if (b == 0) return a;
return gcd(b, a % b);
}

void dfs(int x, __int128 first, __int128 second) {
if (u[x].empty()) {
if (ans[x].first && ans[x].second) {
if (ans[x].second == second) {
ans[x].first += first;
__int128 tmp = gcd(ans[x].first, ans[x].second);
ans[x].first /= tmp;
ans[x].second /= tmp;
}
else {
first *= ans[x].second;
ans[x].first *= second;
ans[x].first += first;
ans[x].second *= second;
__int128 tmp = gcd(ans[x].first, ans[x].second);
ans[x].first /= tmp;
ans[x].second /= tmp;
}
}
else {
ans[x].first = first;
ans[x].second = second;
}
return;
}
int s = u[x].size();
second *= s;
for (int i = 0; i < s; i++) {
dfs(u[x][i], first, second);
}
}

int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> d;
for (int j = 1; j <= d; j++) {
cin >> t;
u[i].push_back(t);
}
}
for (int i = 1; i <= m; i++) {
dfs(i, 1, 1);
}
for (int i = 1; i <= n; i++) {
if (u[i].empty()) {
cout << ans[i].first << ' ' << ans[i].second << endl;
}
}
return 0;
}