#DSU0000028. Học ngôn ngữ (Learning Languages)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Học ngôn ngữ (Learning Languages)
Learning Languages
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A company employs people and uses languages numbered from to . Every employee knows some of these languages, possibly none.
Two employees can communicate directly if they share a language. Communication may also be indirect: an employee can translate for two others, and a chain of such intermediaries is allowed. The company wants every pair of employees to be able to communicate, directly or indirectly.
The company may teach an employee a language they do not already know. Each such lesson costs . Find the minimum number of lessons needed to make communication possible between every pair of employees. An employee does not need to learn a language twice.
Input
The first line contains . The next lines each contain and distinct language indices known by employee .
Output
Print the minimum total cost.
Subtasks
- Subtask 1 (20 points): ; ; the languages listed for an employee are distinct and belong to .
- Subtask 2 (30 points): ; ; the languages listed for an employee are distinct and belong to .
- Subtask 3 (50 points): ; ; the languages listed for an employee are distinct and belong to .
Examples
Input
2 2
1 2
0
Output
1
Explanation
Employee 1 knows language 2; employee 2 knows none. Teaching language 2 to employee 2 costs exactly 1.