#DSU0000027. Phiên dịch ngôn ngữ (Interpretation)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phiên dịch ngôn ngữ (Interpretation)
Interpretation
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A festival has participants and uses languages numbered from to . Participant knows distinct languages, listed in the input.
Two people can communicate directly if they share at least one language. They may also exchange information indirectly through one or more intermediaries: each intermediary only needs a shared language with the preceding person and a possibly different shared language with the next person. The entire chain need not speak a single common language.
Determine whether information can be passed between every pair of participants through direct communication or a chain of intermediaries. Every participant knows at least one listed language.
Input
The first line contains . Each of the following lines starts with , followed by exactly language IDs.
Output
Print YES if all participants can communicate, otherwise print NO.
Subtasks
- Subtask 1 (20 points): , , ; ; each person’s language IDs are distinct and lie in .
- Subtask 2 (30 points): , , ; ; each person’s language IDs are distinct and lie in .
- Subtask 3 (50 points): , , ; ; each person’s language IDs are distinct and lie in .
Examples
Input
4 6
3 1 2 3
2 4 2
2 4 6
1 6
Output
YES
Explanation
Participants 1 and 2 share language 2, participants 2 and 3 share language 4, and participants 3 and 4 share language 6. Thus everyone belongs to one communicating group.