#abc293d. D - Tying Rope

D - Tying Rope

Score : 400400 points

问题描述

设有 NN 根编号为 11NN 的绳子。每根绳子的一端被涂成红色,另一端被涂成蓝色。

你将要进行 MM 次绳结操作。在第 ii 次操作中,你将绳子 AiA_i 上涂有颜色 BiB_i 的一端与绳子 CiC_i 上涂有颜色 DiD_i 的一端打结,其中 R 表示红色,B 表示蓝色。对于每根绳子,相同颜色的一端不会多次被打结。

找出经过所有操作后形成循环的连通绳组的数量,以及不形成循环的连通绳组的数量。

这里,一组连通绳子 {v0,v1,,vx1}\lbrace v_0, v_1, \ldots, v_{x-1} \rbrace 如果可以重新排列集合 vv 中的元素,使得对于每个 0i<x0 \leq i < x,绳子 viv_i 与绳子 v(i+1)modxv_{(i+1) \bmod x} 打结,则称其形成了一个循环。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN ropes numbered 11 through NN. One end of each rope is painted red, and the other is painted blue.

You are going to perform MM operations of tying ropes. In the ii-th operation, you tie the end of rope AiA_i painted BiB_i with the end of rope CiC_i painted DiD_i, 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 {v0,v1,,vx1}\lbrace v_0, v_1, \ldots, v_{x-1} \rbrace is said to form a cycle if one can rearrange the elements of vv so that, for each 0i<x0 \leq i < x, rope viv_i is tied to rope v(i+1)modxv_{(i+1) \bmod x}.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0M2×1050 \leq M \leq 2 \times 10^5
  • 1Ai,CiN1 \leq A_i, C_i \leq N
  • $(A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j)$ (ij)(i \neq j)
  • (Ai,Bi)(Cj,Dj)(A_i, B_i) \neq (C_j, D_j)
  • N,M,AiN, M, A_i, and CiC_i are integers.
  • BiB_i is R or B, and so is DiD_i.

Input

The input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1 C1C_1 D1D_1

A2A_2 B2B_2 C2C_2 D2D_2

\vdots

AMA_M BMB_M CMC_M DMD_M

Output

Print XX and YY in this order, separated by a space, where XX is the number of groups of connected ropes that form cycles, and YY 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: {1}\lbrace 1 \rbrace, {2,4}\lbrace 2,4 \rbrace, and {3,5}\lbrace 3,5 \rbrace.

The group of ropes {3,5}\lbrace 3,5 \rbrace forms a cycle, while the groups of rope {1}\lbrace 1 \rbrace and ropes {2,4}\lbrace 2,4 \rbrace do not. Thus, X=1X = 1 and Y=2Y = 2.

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