#abc293d. D - Tying Rope
D - Tying Rope
Score : points
问题描述
设有 根编号为 到 的绳子。每根绳子的一端被涂成红色,另一端被涂成蓝色。
你将要进行 次绳结操作。在第 次操作中,你将绳子 上涂有颜色 的一端与绳子 上涂有颜色 的一端打结,其中 R
表示红色,B
表示蓝色。对于每根绳子,相同颜色的一端不会多次被打结。
找出经过所有操作后形成循环的连通绳组的数量,以及不形成循环的连通绳组的数量。
这里,一组连通绳子 如果可以重新排列集合 中的元素,使得对于每个 ,绳子 与绳子 打结,则称其形成了一个循环。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are ropes numbered through . One end of each rope is painted red, and the other is painted blue.
You are going to perform operations of tying ropes. In the -th operation, you tie the end of rope painted with the end of rope painted , where R
means red and B
means blue. For each rope, an end with the same color is not tied multiple times.
Find the number of groups of connected ropes that form cycles, and the number of those that do not, after all the operations.
Here, a group of connected ropes is said to form a cycle if one can rearrange the elements of so that, for each , rope is tied to rope .
Constraints
- $(A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j)$
- , and are integers.
- is
R
orB
, and so is .
Input
The input is given from Standard Input in the following format:
Output
Print and in this order, separated by a space, where is the number of groups of connected ropes that form cycles, and is the number of those that do not.
Sample Input 1
5 3
3 R 5 B
5 R 3 B
4 R 2 B
Sample Output 1
1 2
There are three groups of connected ropes: , , and .
The group of ropes forms a cycle, while the groups of rope and ropes do not. Thus, and .
Sample Input 2
7 0
Sample Output 2
0 7
Sample Input 3
7 6
5 R 3 R
7 R 4 R
4 B 1 R
2 R 3 B
2 B 5 B
1 B 7 B
Sample Output 3
2 1
update @ 2024/3/10 12:12:40