题目描述
Problem Description
There are $ M $ types of languages on a certain planet, numbered from $ 1 $ to $ M $.
In a certain year's CODE FESTIVAL on this planet, $ N $ participants gathered from all over the planet.
The $ i $-th participant ( $1 \leq i \leq N$ ) can speak $ K_i $ languages: $ L_{i,1}, L_{i,2}, ..., L_{i,K_i} $.
Two people can communicate only if one of the following conditions is met:
- Both can speak at least one common language.
- There exists a person $ X $ such that both can communicate with $ X $.
Determine whether all $ N $ participants can communicate with each other.
Input Format
The input is given from the standard input in the following format:
$ N $ $ M $ $ K_1 $ $ L_{1,1} $ $ L_{1,2} $ $ ... $ $ L_{1,K_1} $ $ K_2 $ $ L_{2,1} $ $ L_{2,2} $ $ ... $ $ L_{2,K_2} $ $ : $ $ K_N $ $ L_{N,1} $ $ L_{N,2} $ $ ... $ $ L_{N,K_N} $
Output Format
If all $ N $ participants can communicate with each other, output YES; otherwise, output NO.
Sample Input and Output
Sample Input #1
4 6
3 1 2 3
2 4 2
2 4 6
1 6
Sample Output #1
YES
Sample Input #2
4 4
2 1 2
2 1 2
1 3
2 4 3
Sample Output #2
NO
Notes/Hints
Constraints
- $2 \leq N \leq 10^5$
- $1 \leq M \leq 10^5$
- $1 \leq K_i \leq M$
- The sum of $ K_i $ is at most $ 10^5 $
- $1 \leq L_{i,j} \leq M$
- $ L_{i,1}, L_{i,2}, ..., L_{i,K_i} $ are distinct.
Partial Points
- If you solve the dataset satisfying $N \leq 1000$ and $M \leq 1000$ and the sum of $ K_i $ is at most $ 1000 $, you will get $ 200 $ points.
- If you solve the dataset without additional constraints, you will get an additional $ 200 $ points.
Sample Explanation 1
Any two people can communicate as follows:
- Person 1 and Person 2: They can speak the common language 2.
- Person 2 and Person 3: They can speak the common language 4.
- Person 1 and Person 3: Both can communicate with Person 2.
- Person 3 and Person 4: They can speak the common language 6.
- Person 2 and Person 4: Both can communicate with Person 3.
- Person 1 and Person 4: Both can communicate with Person 2.
Note that there might be languages that no one can speak.
Sample Explanation 2
For example, Person 1 and Person 3 cannot communicate.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分