#DSU0000030. Buộc dây thành vòng (Tying Rope)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Buộc dây thành vòng (Tying Rope)
Tying Rope
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are ropes numbered from to . Each rope has two distinct ends: one red (R) and one blue (B). Initially, the ropes are separate.
Exactly knots are tied. Each knot joins two previously unused ends. The ends may belong to different ropes or to the same rope. Each specific end is used in at most one knot across all operations. After all knots have been tied, some ropes form connected groups.
A group is a closed loop if following its rope segments and knots forms a cycle without encountering a free end. All other groups are open ropes. Count the number of closed-loop groups and the number of open-rope groups.
Input
The first line contains . Each of the next lines contains , tying end of rope to end of rope .
Output
Print two integers : the number of cyclic and non-cyclic groups.
Subtasks
- Subtask 1 (20 points): , ; ; ; ; each rope end is tied at most once.
- Subtask 2 (30 points): , ; ; ; ; each rope end is tied at most once.
- Subtask 3 (50 points): , ; ; ; ; each rope end is tied at most once.
Examples
Input
5 3
3 R 5 B
5 R 3 B
4 R 2 B
Output
1 2
Explanation
The two knots between ropes and use all four ends, forming a closed loop. Ropes and form one open chain, while rope remains a separate open chain. Thus there is one cyclic component and two non-cyclic components.