The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages.
Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces.
Output
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
Examples
Input
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
Output
0
Input
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
Output
2
Input
2 2
1 2
0
Output
1
Note
In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.
In the third sample employee 2 must learn language 2.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to read the problem and understand: There are n employees, m languages. Each employee knows a set of languages (possibly empty). Employees can learn any language at cost 1 per language per employee (i.e., each course costs 1 berdollar, so teaching one language to one employee costs 1). We want to achieve that any employee can correspond with any other employee, either directly (they share a common language) or indirectly through other employees (i.e., the graph of employees where edges exist if they share a common language must be connected). Actually, translation: They can communicate if there is a path of employees such that each adjacent pair shares a language (i.e., they can translate). So we need the employees to be in a single connected component via language sharing. We can teach languages to employees to create edges.
Goal: minimize total number of language courses (i.e., number of new language assignments to employees) such that the resulting graph is connected.
We have n up to 100, m up to 100.
We can think in terms of languages as also nodes? There's a bipartite graph: employees and languages, edges if employee knows language. Communication: Two employees can communicate if they share a language (direct edge) or through chain of languages. This corresponds to the graph of employees being connected via the language nodes: If we consider a bipartite graph, then two employees are connected if there is a path between them in this bipartite graph (alternating employees and languages). This is equivalent to the employees being in the same component of this bipartite graph. So the condition we need is that all employees belong to the same connected component of the bipartite graph (including languages). Since languages are also nodes, we can add new edges by teaching languages to employees. Adding an edge means teaching a language to an employee; each such edge costs 1.
We want to add the minimum number of edges to connect the whole bipartite graph (i.e., make the graph connected across all employees). Actually, we only care about employees being connected; languages are just intermediate. So we need the employee nodes to be in one connected component.
We can model the current knowledge as a bipartite graph with n employee nodes and m language nodes. Initially there may be isolated nodes (employees with no languages, languages known by no one). We can add edges (employee, language) at cost 1 each. The objective: after adding edges, the whole graph (employees+ languages) must have all employees in one connected component. However, languages may remain isolated? If a language is isolated (no employee knows it) after adding edges, that's fine, because it doesn't affect connectivity among employees. But if we add an edge to a language that no one else knows, that may not help connectivity unless we also connect it to others. Usually we consider that adding an edge can connect two components: the component containing the employee and the component containing the language. So if a language is isolated (no edges), we can add an edge to an employee, that language becomes part of that employee's component. But if we want to use that language to connect two employee components, we need at least two employees to learn that language (or one employee from each component to learn the language). So essentially we need to connect the employee components using languages as bridges.
We can think of the initial graph: we have some connected components (including languages). Let's denote components in the bipartite graph. Within each component, all employees are mutually reachable via languages. So the employees are partitioned into groups: employees in the same component can already communicate with each other (maybe indirectly through chain of languages and other employees). So the problem reduces to connecting these employee groups. Also, note that an employee that knows zero languages is isolated (i.e., it's a component of just that employee, because no languages). But it's an employee node with no edges; such a component contains only that employee. Similarly, a language known by no one is isolated as a single language node.
Goal: using edge additions (cost 1 per edge), we want to make the graph such that all employees are in one component. The minimal number of edges to add to connect a set of components? This is reminiscent of connecting a graph: given a graph with k connected components, the minimum number of edges to add to make it connected is k-1, but only if we can add edges arbitrarily between any two nodes? Here we can only add edges between an employee and a language. So we cannot directly connect two employee components by just adding an edge between two employees; we need to go through a language. However, we can create a new language and have employees learn it, but languages are fixed (m languages exist). Actually, we have m languages available. They are numbered 1..m. So we can only use those languages. But we can teach any language to any employee. So we can use any of the m languages as a bridge. However, we might also need to consider that languages themselves are nodes; if a language is not present in any component, it's isolated. We can "activate" a language by teaching it to some employee.
So essentially we have k components (among employees + languages). Let's call the initial components of the bipartite graph. Among these components, some contain employees, some only languages. We want to connect all employee nodes into one component. This is akin to: we have a set of "employee components" (each component that contains at least one employee). Let p = number of such components. We need to connect these p components. To connect p components in a bipartite graph where we can only add edges between an employee and a language, what's the minimum number of edges?
We can think: If we had a language that is known by employees from different components, then those components become connected. But initially, languages are only in one component. So we can add edges to bring a language into multiple components, effectively merging them.
Consider we have a language L. Initially it may belong to some component, or it may be isolated. If we teach L to an employee in component A, then L becomes part of component A (if L was isolated, it joins A; if L belonged to another component B, then teaching L to an employee in A would merge A and B, because now L connects the two components). So adding an edge between employee in component A and language L can merge A with the component containing L. If L is isolated, then after adding one edge, L becomes part of A, but that doesn't merge with any other employee component unless later we teach L to another employee from a different component. So to merge two employee components A and B using language L, we need to have at least one employee from each learn L, or have L initially in one component and teach it to an employee in the other. In either case, we need to add edges such that L is incident to at least one employee from each component we want to merge.
Thus, connecting p employee components can be done by selecting a language (maybe existing or new? but languages are fixed) and teaching it to at least one employee from each component. But if we use a language that is initially in one of the components, we may need to add edges to all other components to that language. Alternatively, we could use multiple languages as intermediate connectors.
We need to find the minimal number of edge additions to connect all employee nodes. Since edge additions are cheap and each edge connects an employee and a language, we might be able to connect everything with p-1 edges? Let's test with examples.
Example 1: n=5, m=5, lists: employee1 knows 2; employee2 knows 2,3; employee3 knows 3,4; employee4 knows 4,5; employee5 knows 5. This yields a chain: employees share languages, all are connected. So p=1, cost 0. That matches.
Languages: 1..7.
Edges: Emp2 -1,2,3; Emp3 -1; Emp6 -3; Emp8 -1. So employees 2,3,6,8 and languages 1,2,3 are in one component (say Comp A). Emp4 knows 5,4; Emp7 knows 7,4; Emp5 knows 6,7. Emp4 and Emp7 share language 4? Emp4 knows 4, Emp7 knows 4, so they are directly connected. Emp7 also knows 7, Emp5 knows 7, so Emp5 connects via 7. Also Emp4 knows 5, but nobody else knows 5. Emp5 knows 6, but nobody else knows 6. So employees 4,5,7 and languages 4,5,6,7 are in another component (Comp B). Employee1 knows nothing, isolated (Comp C, just employee1). So there are three employee components: A (employees 2,3,6,8), B (4,5,7), C (1). So p=3. The answer is 2. So with 2 edges we can connect them. How? They gave: employee1 can learn language 2, employee8 can learn language 4. Actually employee8 is in A, learns language 4 (which is in B). That connects A and B via language 4 (now language 4 is in B; if employee8 learns 4, then that merges A and B). Then employee1 learns language 2 (which is in A). That merges C with the combined component. So total edges = 2. That is p-1 = 2. So indeed p-1 edges sufficed.
Example 3: n=2, m=2; employee1 knows 2; employee2 knows 0. So components: employee1 and language2 are in one component (Comp A). Employee2 isolated (Comp B). p=2. Answer is 1, which is p-1.
So it seems answer might be p-1 if there is at least one language known by someone? But consider edge case: if all employees know no languages, i.e., every employee is isolated and there are no languages known? Actually languages exist but no one knows them. In that case, each employee is a separate component (since no edges to languages). Also languages are isolated. So we have n employee components. To connect them, we need to create a connected structure. Since we can only add edges between employees and languages, we need to use languages as bridges. But we have m languages available. The cheapest way to connect n isolated employee nodes? We could pick one language, say language 1, and teach it to all employees. That would cost n edges. That would connect all employees via that language (since they all share language 1). But is that minimal? Possibly we could use a chain: teach language1 to employee1 and employee2, connecting them; then teach language2 to employee2 and employee3, connecting, etc. That would cost n edges as well? Actually, to connect n isolated nodes in a tree using languages as intermediate nodes, we can treat languages as additional nodes that can be used to connect employees. In a bipartite graph where left side employees (n) and right side languages (m), we want to make the graph connected (all employees in one component). The minimal number of edges to add to connect all employees? This is like we have n+m nodes, initially isolated (if no edges). To have all employee nodes connected, we need to construct a connected graph that includes all employees (languages may or may not be used). The minimal number of edges to make a graph with n+m vertices connected is (n+m)-1, but we already have some edges? Actually initially there are 0 edges. So to make the whole bipartite graph connected (including languages) we need at least (n+m)-1 edges. But we only care about employees being connected; languages can be isolated if they are not used. However, employees alone cannot connect directly; edges must be between employee and language. So to connect two employees we need at least one language node that connects them. So essentially the employee nodes must be linked via language nodes. The minimal number of edges to connect n employee nodes in a bipartite graph where we can use up to m language nodes is: if we use k language nodes, we need at least n + k - 1 edges? Let's think: Suppose we build a tree that includes all employees and some languages. Since it's bipartite, edges only between employee and language. A tree that spans all employee nodes (and possibly some languages) would have number of nodes = n + t (where t languages used), and edges = (n + t) - 1. Each edge is a course (teaching). So the total cost = edges added (since initial edges are zero). But we could also use existing edges if any. However, if there are zero initial edges, we need to add at least n edges? Because a tree on n employees using languages as intermediate: each leaf employee must have at least one edge. In a tree that includes all employees, each employee node must have degree at least 1 (except maybe if there's only one employee). In a bipartite tree with employee nodes and language nodes, the tree must have all employees; the number of edges is (n + t) - 1. Since each edge connects one employee and one language, the total number of edges incident to employees is exactly the number of edges (since each edge incident to exactly one employee). So the sum of degrees of employees = edges. For n employees, minimum total edges is n (if the tree is like a star: one language node connected to all employees, then t=1, edges = n+1-1 = n). That matches: teach one language to all n employees: cost n. That seems minimal because you need at least n edges to give each employee at least one language (unless some employee already knows a language, but here none know any). However, is it possible to connect n employees with less than n edges? Suppose we have employees A and B. To connect them, we need at least one language node that both know; that requires two edges (A learns L, B learns L). For three employees, to connect them all, we could have A learn L, B learn L, C learn L, that's 3 edges (star). Could we do with 2 edges? For example, A learns L1, B learns L1 (2 edges) connects A and B. Then C learns L1 as well? That would be third edge. If we try a chain: A learns L1, B learns L1 and L2, C learns L2. That's A-L1 (edge), B-L1 (edge), B-L2 (edge), C-L2 (edge) -> 4 edges. Not better. So indeed each employee must be incident to at least one added edge (unless they already have an edge from initial knowledge). So minimal cost is at least the number of employees with zero known languages? Not exactly, because some employees may already know languages and thus are already connected in components. So we need to connect the components.
Given the examples, the answer appears to be p-1, where p is the number of employee components. But is that always sufficient? Let's analyze.
We have p components that each contain at least one employee. Within each component, there may be languages known. Also there are languages isolated (in no component). We want to merge these p components into one.
We can do this by adding p-1 edges, each edge connecting an employee from a component to a language that is either in another component or an isolated language that we then connect to another component. For instance, we can pick one component as root, and for each other component, we add an edge between an employee in that component and a language in the root component (or any language that is already in the merged component). But is it always possible to add just one edge to connect a new component? To connect component A to component B (which already merged), we need an edge that connects an employee in A to a language in B (or vice versa). But does B always have a language node? If B consists solely of employees? But bipartite components: if a component has employees, it must also have languages if any employee knows a language? Actually, consider a component that consists only of employees? Is that possible? In the bipartite graph, edges are only between employees and languages. If there are employees with no languages, they are isolated nodes (no edges). That component contains only that employee (since no language attached). So a component with only employees and no languages can occur only if it's an isolated employee with zero known languages. So that's a component with just an employee node. For such a component, there is no language node inside it. To connect this component to others, we need to add an edge from that employee to some language that is in another component (or to an isolated language, but then we still need to connect that language to other components). If we add an edge from that employee to a language in another component, that merges them, costing 1 edge. So that's fine.
Now consider a component that has employees but no languages? Is that possible if there is at least one employee with a language? No, because any employee with a language has an edge to that language, so that language is part of the component. So the only employee-only components are isolated employees with zero languages. So any component with at least one employee that knows at least one language includes at least one language node. Good.
So for each component that has at least one language, we can select a language from that component as a "connector". For components that are isolated employees (zero languages), they have no language, but we can teach them a language that is already in the merged component.
Thus, we can connect p components with p-1 edges: pick a component (say component 1) as base. For each other component i, we can add one edge: either from an employee in component i to a language in the base (if component i has at least one employee that can learn that language), or from an employee in base to a language in component i (if component i has a language). Both are possible as long as we can choose a language that exists. Since languages are numbered 1..m, any language can be taught. So we can always pick a language that is present in the base component (or in component i). But we need to ensure that the language we choose is either already known by someone in the base (so it belongs to base) or we can teach it to the base as well? Actually, if we add an edge from an employee in component i to a language L that is in base, then L is already in base, and teaching employee i L merges the two components. That's valid. For this to be possible, L must be a language that exists (any language). But if base has no languages? Base component could be an isolated employee with zero languages. Then base has no language nodes. In that case, we cannot pick a language from base because base has none. However, we could instead add an edge from an employee in base to a language in component i (if component i has languages). Or we could use an isolated language as a bridge: teach base employee some language L, and also teach employee i the same L, requiring two edges. But we want to do it with one edge per extra component. So we need to handle the case where the base component has no language.
Let's examine: if there is at least one component that contains a language, we can choose that as base, and then for each other component (including isolated employee components) we can add one edge: if component i has at least one language, we can pick an employee from base to learn one of its languages? Actually, to connect with one edge, we need an edge that connects the two components. That means either: employee in A learns a language from B, or employee in B learns a language from A. Both require that the target component (the one providing the language) has a language. So if both components have languages, either direction works. If one component has no languages (i.e., isolated employee), then the only way to connect with one edge is to have that employee learn a language from the other component (since the other component has languages). That works: employee from isolated component learns a language that is in the base (which has languages). So if base has languages, connecting isolated employees costs 1 edge each. Good.
But if base component has no languages (i.e., it's an isolated employee with zero languages) and we also have other components, some of which may have languages. To connect base (isolated) to a component with languages using one edge, we could have the base employee learn a language from that component, costing 1 edge. That works: base employee learns a language that is in component with languages. So after that, base now has that language, so the merged component now has languages. Then we can connect other components similarly. So it seems that connecting any two components can always be done with 1 edge, as long as at least one of the components has a language. But if we have two components both without languages? That would be two isolated employees (each knows zero languages). To connect them, we need to add edges. They have no languages. So we need to introduce a language. We could teach one of them a language L, and the other the same language L. That would require 2 edges to connect them. But can we do it with 1 edge? If we add an edge from employee A to language L (L is not in any component, maybe an isolated language or we consider a new language? But languages are nodes; if L is isolated (no one knows it), then after A learns L, L becomes part of A's component (now A and L). But B is still isolated. To connect B, we need B also learn L (or some other linking). So 1 edge cannot connect two employee-only components because there is no language node shared. So if we have two or more isolated employees (zero languages) and no other components that have languages, then we need to spend at least 2 edges to connect two of them? Actually, to connect k isolated employees (with no languages known initially), what is the minimal number of edges needed? Let's consider k isolated employees, and there are m languages available (isolated as well). We can choose a language L and teach it to all k employees. That costs k edges, and connects them all. Could we do with k-1 edges? Suppose we have employees A, B, C. Could we connect them with 2 edges? For example: teach A language L1; teach B language L1 (now A and B are connected via L1). C is still isolated. To connect C, we need an edge. Could we connect C to the A-B component with 1 edge? If we teach C language L1, that's a third edge. If we teach C language L2 and also teach someone else L2? That would be extra edges. So seems we need at least k edges to give each isolated employee at least one language, because initially they have degree 0. After adding edges, each employee must have degree at least 1 to be connected to the rest (unless the component has only one employee? But if k>1, each employee must be incident to at least one edge in the final connected graph, otherwise they'd be isolated). So minimal edges is at least k. But can we achieve connectivity with exactly k edges? Yes, by teaching the same language to all k employees: that's k edges and yields a star centered on that language, connecting all employees. So minimal cost for k isolated employees is k. But note: if there are other components that already have languages, we might not need to spend that many edges on these isolated employees; we can connect each with 1 edge (teach them a language from the main component). So for each isolated employee, if there exists any component with languages, we can connect them with 1 edge each. That yields cost = number of isolated employees (if we connect each individually). But what if there are multiple isolated employees and no component with languages? That means all employees have zero languages (n employees, each knows 0). And m could be anything. Then the graph has n isolated employee nodes and m isolated language nodes. To connect all employees, we need to make them all in one component. The minimal edges: we can pick one language, teach it to all n employees, costing n edges. Could we do better using multiple languages? Possibly with fewer than n edges? Let's think: we need to end with a connected graph that includes all employees. The graph is bipartite. The number of edges in any connected bipartite graph with n employees and some languages is at least n (since each employee must have degree at least 1). So lower bound = n. And we can achieve n. So answer would be n.
But what about the p-1 formula? In this scenario, p = number of employee components = n (since each employee isolated). p-1 = n-1. But the answer needed is n, not n-1. So p-1 is not sufficient when there are no languages known initially at all. But is that scenario possible? The problem states each employee may know zero languages. So yes. In the sample inputs, there were cases with zero languages for some employees, but there were others that knew languages, so overall there was at least one language known. But if all employees know zero, then we have n isolated employee components, and also m isolated language components. p = n. If we try to connect with p-1 edges = n-1, can we achieve connectivity with n-1 edges? Suppose we try to build a tree that includes all employees and some languages. If we use t language nodes, total nodes = n + t, edges = (n+t)-1. But the number of edges incident to employees must be at least n (since each employee must have degree >=1). Actually, in a tree, the sum of degrees of employees = number of edges (since each edge contributes 1 to employee degree). If we have n employees, the sum of degrees of employees = edges (because each edge is incident to exactly one employee). So sum(deg(employee_i)) = E. For all employees to have degree >=1, we need E >= n. But if we have E = n-1, that would sum of degrees = n-1, which by pigeonhole would force at least one employee to have degree 0, meaning that employee is isolated, not connected. So indeed we need at least n edges. So p-1 works only if each component (except possibly one) contains a language node to connect with a single edge. More precisely, if there is at least one language known overall (i.e., there exists at least one edge initially), then we can use that language as a connector and p-1 edges suffice? Let's test with scenario: n=2, m=1, employee1 knows 1, employee2 knows 0. So initial components: component A: employee1 + language1; component B: employee2 alone. p=2. There is at least one language known (language1). According to p-1 =1, answer 1. That matches sample 3? Actually sample 3: employee1 knows 2, employee2 knows 0, answer 1. So yes.
What about n=3, m=2: employee1 knows 1, employee2 knows 2, employee3 knows 0. So components: A: emp1 + lang1; B: emp2 + lang2; C: emp3 alone. p=3. There is at least one language known? Yes, two. p-1=2. Can we connect with 2 edges? Possibly: teach emp3 language1 (cost1) connects C to A. Now we have A+C and B separate. To connect B, we could teach emp2 language1 (cost2) merges B with A+C. Total 2 edges. Works. Or teach emp1 language2, etc. So 2 edges works.
Now consider n=3, all employees know zero: p=3. p-1=2 but we argued we need at least 3 edges. Let's test if we could connect with 2 edges. Suppose we teach emp1 language1, emp2 language1 (cost2). Now emp1 and emp2 are connected via language1; emp3 is isolated. So not connected. Could we teach emp3 language1? That's third edge. Could we do something like teach emp1 language1, and teach emp3 language1? That connects emp1 and emp3, emp2 isolated. No. So indeed need at least 3. So answer should be n if there are no languages known at all.
But is there any other scenario where p-1 is not enough? Suppose we have components that have no languages: but those are isolated employees only. As argued, if we have at least one component that has a language, we can connect each isolated employee with 1 edge. So p-1 would be: number of components = (number of components containing at least one employee). Among these, let s be the number of components that are isolated employees (no language). Then p = c_lang_components + s, where c_lang_components is the number of components that contain at least one language and at least one employee (since any component with a language also has at least one employee that knows it). Actually, a component could consist solely of language nodes? That can happen if there is a language that no employee knows? But initially, a language node with no edges is isolated, forming a component with just that language. That component has no employees. But we might ignore it because it doesn't contribute to employee components. However, such isolated language nodes can be used to connect components without cost? They are free nodes that we can use. To connect two employee components using an isolated language, we need to add two edges: one from an employee in first component to that language, and one from an employee in second component to that language. That costs 2 edges to connect two components, whereas if we use a language already in a component, we can do with 1 edge. So the presence of isolated languages could affect the minimal edges if there are no components with languages. But if there is at least one component with a language, we can use that language to connect others, so isolated languages are not needed. But maybe there are components that have languages but we might not be able to access them due to some constraints? Since we can teach any language to any employee, we can always teach an employee a language that is in another component, because languages are just identifiers; we are not limited. So we can always choose a language that appears in some component, and teach it to an employee from another component. That adds an edge and merges the two components. So as long as there is at least one component that contains a language (i.e., at least one employee knows a language), we can connect all other components with exactly one edge each. So answer would be p-1.
But wait, what about components that consist solely of languages (no employees)? They are irrelevant; we can ignore them because they don't contain employees. However, they might be used to reduce cost? But they are not necessary because we can always use a language from an employee component. Unless there is no employee component that contains any language. That is the case when all employees know zero languages. Then all employee components are isolated employees. In that case, there is no component with a language that also has an employee. There might be isolated language nodes, but they are separate components with no employees. To connect employee components, we must bring in a language. We can use an isolated language to serve as a hub. But then to connect two isolated employees to the same language, we need two edges. However, to connect all n isolated employees, we could use one language and teach it to all n employees: that's n edges. Could we use multiple languages to reduce edges? For n employees, we need at least n edges as argued. So answer = n.
But is there any scenario where p-1 fails even if there is at least one language known? Consider a component that contains a language but we cannot "reach" it? No, we can always teach that language to anyone. So yes.
Thus, answer = (number of employee components) - 1 if there is at least one employee who knows at least one language; else answer = n (since each employee component is an isolated employee, and there are n of them). But careful: could there be a scenario where some employees know languages, but there are also isolated employees, and the component with languages maybe consists only of languages? That can't happen because a language known by an employee will be in a component with that employee. So there will be at least one employee component with languages. So the condition "there is at least one employee who knows at least one language" is equivalent to "there exists at least one edge in the initial graph". Or more directly: if total number of known languages (sum of ki) > 0, then answer = (number of employee components) - 1. However, note that it's possible that some employees know languages, but all those languages are known only by those employees, but still there is at least one edge. So the condition holds.
But consider a case: n=2, m=1, employee1 knows 1, employee2 knows 1. Then they are in same component, p=1, answer 0. That's consistent.
Now consider a more tricky case: there are employees that know languages, but all those languages are known only by single employees and the employees don't share languages, so they are in separate components, each with its own language(s). For example, employee1 knows language1 only; employee2 knows language2 only; employee3 knows 0. So components: A: emp1 + lang1; B: emp2 + lang2; C: emp3 alone. p=3. There is at least one language known. According to formula, answer = 2. And we can: teach emp3 language1 (cost1) merges C with A. Now we have A+C and B. Then teach emp2 language1 (cost2) merges B. So 2 edges. Works.
Now, consider if there is a language known by multiple employees, they are in same component, so p reduces.
Thus, the answer seems to be: compute the number of connected components among employees and languages (considering the bipartite graph). Let comp be the number of components that contain at least one employee. Additionally, check if there is any language known at all (i.e., total number of languages known > 0). If yes, answer = comp - 1; else answer = n (since all employees isolated and no languages known). But is it ever that comp = 0? No, n >= 2, so there is at least one employee.
But wait: what if there are employees that know languages, but those languages are all isolated? That doesn't make sense because if an employee knows a language, that language is connected to that employee. So it's not isolated.
So the algorithm: Build bipartite graph with n + m nodes. For each employee i (1-indexed), for each language l in his list, add an edge between employee node i and language node n+l (or offset). Then compute connected components via BFS/DFS. Count comp_emp = number of components that contain at least one employee. Also, check if there is any edge at all (or sum ki > 0). If sum ki == 0, answer = n; else answer = comp_emp - 1.
Let's test with sample 2: sum ki = employee1:0, emp2:3, emp3:1, emp4:2, emp5:2, emp6:1, emp7:2, emp8:1 => total = 12 >0. comp_emp = 3 (A, B, C) so answer=2. matches.
Sample 1: sum ki >0, comp_emp? All employees in one component? Let's see: employees are connected via languages. So comp_emp=1, answer=0.
Now consider case: n=3, m=3, all employees know 0. sum ki=0, answer = n =3. Let's test if we can do with 3: teach language1 to all three: cost 3, connectivity achieved.
But could there be a scenario where sum ki>0 but there exists an employee component with no languages? That's the isolated employee(s). That's fine.
But what about scenario where there are languages known, but all employee components are isolated employees? That would require that each employee knows zero languages, but there is at least one language known? But if a language is known, some employee knows it, so that employee is not isolated. So it's impossible to have sum ki>0 and all employee components being isolated employees (since the employee who knows a language belongs to a component with that language, thus that component contains a language). So at least one employee component has a language. So our formula works.
But is there a scenario where sum ki>0 but comp_emp - 1 could be less than number of isolated employees? For example, many isolated employees and one component with languages. comp_emp = 1 (big component) + number of isolated employees. So answer = isolated_employees. That's exactly the cost: teach each isolated employee a language from the big component (one edge each). So that matches.
Now consider if there are multiple components each with languages, but no isolated employees. Then comp_emp = number of such components, answer = components-1, which is the number of edges needed to connect them, each edge connects a language from one component to an employee from another. That works.
Thus, the answer seems straightforward.
But wait, the problem statement says "The employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar." So they can learn any language, but is there any restriction that an employee can only learn a language once? It's fine. And we want minimum total courses. Our approach is to add edges.
But is it always optimal to connect components with p-1 edges when sum ki>0? Let's think: Could there be a scenario where it's cheaper to use two edges to connect three components rather than using two edges each connecting a pair? That doesn't make sense because p-1 is the minimum number of edges needed to connect p components in a graph, given we can add edges arbitrarily. But since edges can only be between employee and language, maybe connecting p components might require more than p-1 edges if some components lack languages? But we argued that as long as at least one component has a language, each connection can be done with 1 edge. However, what if we have two components both lacking languages? That can't happen because a component lacking languages is an isolated employee, and if there are two isolated employees, they are separate components, but there is no language in either. To connect them, we need at least 2 edges (teach a common language to both). That would be 2 edges to connect 2 components, which is 2 > (p-1=1). So the p-1 formula would underestimate. But can this happen when sum ki>0? Yes, it could: suppose we have one component that has languages (call it A), and two isolated employees (B and C). Then p = 3. p-1 = 2. According to our earlier reasoning, we can connect B to A with 1 edge, and C to A with 1 edge, total 2 edges. That's fine. Now, can we have two isolated employees and no component with languages? That would mean sum ki=0, which we treat separately. So if sum ki>0, there is at least one component with a language, so any isolated employee can be connected with 1 edge. So that's fine.
Now, what about a component that consists only of languages (no employees) that we might want to use? It's irrelevant because it doesn't contain employees, but it might be beneficial if we need to connect employee components that have no languages? But as argued, if there is any employee component with languages, we can use that. If there are no employee components with languages, then all employees are isolated (sum ki=0), and there are only language nodes isolated. Then to connect employees, we must use languages. The minimal edges is n (each employee learns a language). Could we do better by using multiple languages? No.
One more nuance: Could there be a scenario where sum ki>0 but the only component that has languages is such that its languages are all known only by employees within that component, but we cannot teach those languages to employees from other components because... there's no restriction. So we can. So p-1 works.
Thus, answer is: if total known languages > 0, answer = (number of connected components containing at least one employee) - 1; else answer = n.
But we need to be careful: what about employees who know zero languages? They are separate components only if they don't share any language with anyone. That's correct.
Now, we must implement solution. Steps:
Read n, m.
Initialize adjacency list or union-find structure. Since n+m ≤ 200, we can use union-find (DSU) for simplicity. Represent employees as 0..n-1 (or 1..n) and languages as n+1..n+m (or n..n+m-1). We'll have total nodes = n + m.
For each employee i (1-indexed), read ki, then read ki languages. For each language l, union(i-1, n + (l-1))? Use zero-based for DSU. Actually, we can index employees 0..n-1, languages n..n+m-1 (or n..n+m-1). So for language l (1-indexed), node id = n + (l-1). Perform union(i, lang_node).
After processing all, we also need to know which components contain employees. So we can compute for each root, whether there is at least one employee in that component, and also whether there is at least one language? But we only care about employee-containing components count.
To count comp_emp, we can find set of roots for each node that is an employee. Or iterate over all nodes (0..n+m-1), compute root, then mark if that component has any employee. Since we only care about components that have at least one employee. Also we can count total employees per component.
Simplify: For each employee i (0..n-1), find its root. Collect unique roots among employees. That's comp_emp = number of distinct roots. This counts each component that contains at least one employee. (Note: a component with only languages would not be counted, which is fine.)
Compute total_known = sum of ki. If total_known == 0, answer = n. Else answer = comp_emp - 1.
But wait, is it always safe to use total_known == 0? Could there be a situation where total_known > 0 but all known languages are known only by employees that are all in one component? That's fine, comp_emp may be > 1 due to isolated employees. That matches.
Edge case: n=2, m=1, both know 1: total_known=2>0, employee1 and employee2 share language 1, they are in same component (since both connected to lang1). So comp_emp=1, answer=0.
Edge case: n=2, m=2, both know 0: total_known=0, answer = n =2.
Edge case: n=100, m=0? But m ≥ 2 per constraints? Actually constraints: 2 ≤ n, m ≤ 100. So m≥2, so there are languages. But sum ki can be zero.
Now, double-check with a more complex scenario: n=4, m=3. Lists: emp1: 1; emp2: 2; emp3: 3; emp4: 0. So components: A: emp1 + lang1; B: emp2 + lang2; C: emp3 + lang3; D: emp4 alone. comp_emp=4, total_known=3>0, answer=3. Is it possible with 3 edges? Connect emp4 to emp1 via teaching lang1: cost1. Now we have A+D, B, C. Then connect B to A+D by teaching emp2 lang1: cost2. Then connect C to merged by teaching emp3 lang1: cost3. Total 3. Could we do with 2 edges? Possibly by using a language to connect multiple at once? For example, teach emp2 lang1 and emp3 lang1 (2 edges) would connect B and C to A, but emp4 still isolated. We still need an edge for emp4. So 3 needed. So answer matches.
What about using a language that is not initially known? Could we use language 4 (which is not in any component) to connect multiple? For instance, teach emp1 lang4, emp2 lang4, emp3 lang4, emp4 lang4? That would be 4 edges. Not better.
Now consider if there are multiple isolated languages, but we could use one to connect all components with p edges? p-1 is still better.
Thus algorithm appears correct.
But wait, there is nuance: Could there be a scenario where connecting components with p-1 edges is not possible because the component that has languages doesn't have any language that can be taught? No, any language can be taught to any employee, regardless of whether the employee already knows it? The problem says "learn any number of official languages", so they can learn new languages, even if they already know some. So teaching a language that the employee already knows is redundant and not needed. But we can choose a language that exists in the target component; that language is known by at least one employee in that component, but we can teach it to employees from other components. That's allowed. So no issue.
But what if the component with languages only contains languages that are already known by all employees? Not relevant.
Thus, answer formula stands.
But I've seen this problem before (CF 277A?). I recall that the solution is: count employees who know no languages; if everyone knows none, answer = n; else answer = number of connected components (in the graph of employees via language) - 1? Wait, typical solution for CF 277A "Learning Languages" is: Build graph where employees are nodes, and they are connected if they share a language. Actually, I've seen problem "Berland Suit" or something. Let me recall: There's a known Codeforces problem: "The number of employees knowing no language" etc. I think it's CF 277A - Learning Languages. The solution: If there is at least one language known, the answer is (number of connected components among employees) - 1, where employees are connected if they share a language (directly). But wait, is that equivalent to our comp_emp? In that problem, they consider employees as nodes and an edge between two employees if they share a language. Then the number of components among employees (in that graph) is computed. Then if there is at least one employee who knows a language, answer = (components - 1). But if no one knows any language, answer = n. That is exactly what we have? Not exactly: In that model, two employees who are connected via a chain of languages (i.e., employee A knows L1, employee B knows L1 and L2, employee C knows L2) would be in the same component because A shares L1 with B, B shares L2 with C, so there is a path A-B-C. So the graph of employees (connected if they share a language) yields the same components as our bipartite graph's employee components? Let's verify: In bipartite graph, employees are in same component if there is a path alternating employees and languages. In the employee-only graph where edges exist if they share at least one language, connectivity is the same? Not necessarily: If employee A knows L1, employee B knows L2, and there is an employee C that knows both L1 and L2, then A and B are connected via C (A-C and C-B). In the employee graph with edges defined by direct shared language, A and C are directly connected (share L1), C and B directly connected (share L2), so A and B are connected via path A-C-B. So the connectivity in employee graph (with edges defined by direct shared language) is the same as connectivity in the bipartite graph's projection onto employees. However, if there is no intermediate employee, but a language itself can serve as bridge without an intermediate employee? Actually, in bipartite graph, A and B can be connected via a language L if both know L, that's direct. If they don't share a language, they need an intermediate employee who knows a language that A knows and another language that B knows. That's exactly the employee graph connectivity. So yes, the employee graph where vertices are employees and edges between those who share at least one language yields the same connected components as the bipartite graph's employee components. Additionally, isolated employees (knowing zero languages) are isolated nodes in that graph.
Thus, comp_emp is exactly the number of connected components in the graph where nodes are employees and edges exist if they share at least one language. And it's easier to compute: we can build an adjacency based on languages: for each language, collect list of employees who know it, and then union those employees together. That's what we do with DSU on employees only, without language nodes. That's simpler: we have n employees. For each language, for each pair of employees that know that language, union them. Alternatively, we can still use bipartite DSU but count distinct roots among employees. But the employee-only approach yields same comp count.
But careful: In employee-only graph, employees who know no language are isolated, they form components of size 1. So comp_emp includes them. That's fine.
Thus, the typical solution: Count number of employees who know at least one language? Actually, they often count zero-knowledge employees separately. But the final formula: If no employee knows any language, answer = n; else answer = (number of connected components among employees) - 1.
Let's verify with examples: In sample 2, employee-only components: let's compute: Employees sharing languages:
Language1: known by employees 2,3,8 -> union them.
Language2: known by employee2 only? Actually employee2 knows 2, but who else knows 2? Only employee2. So nothing.
Language3: known by employees 2,6 -> union (2 already with 3,8, so now 6 joins).
So employees 2,3,6,8 are in one component.
Language4: known by employees 4,7 -> union them.
Language5: known by employee4.
Language6: known by employee5.
Language7: known by employees5,7 -> union 5 with 4,7.
So employees 4,5,7 are in second component.
Employee1 knows nothing: isolated.
So components = 3. Answer = 2 (since at least one language known). Yes.
In sample 3: employees: 1 knows 2, 2 knows 0. So components: {1}, {2}? Actually, employee1 knows language2, but no other employee knows language2, so component {1} (since no sharing). Employee2 isolated. So components = 2. Since there is at least one language known (employee1 knows one), answer = 1. Good.
Now, edge case: n=2, both know 0: components = 2, but no language known, answer = n = 2.
Thus, solution: Build DSU for employees (0..n-1). For each language, maintain list of employees who know it. For each language, for each pair (or simply union all employees in that list sequentially: take first as representative and union others with it). Then count distinct roots for employees. Also compute a flag if any employee knows at least one language: i.e., if total_known > 0. Then if flag false: print n; else print (components - 1).
But be careful: In some cases, there could be employees who know languages but they are isolated in the employee graph because they are the only one who knows those languages, and there are other employees who also know languages but different languages and no bridging employee? That results in multiple components. So components - 1 edges needed.
But is it always possible to connect components with components - 1 edges when at least one language is known? In employee-only graph, we need to make the graph connected by adding edges (teaching languages). Teaching a language to an employee effectively can create new shared language edges. We can think: Pick a language L that is known by some employee in a component. We can teach L to an employee in another component, thereby creating a shared language between those two employees, effectively adding an edge between the two components. That merges them. One edge per merge. So yes.
But there is a nuance: Suppose we have component A where employees know languages, but all those languages are only known by employees in A. We can teach one of those languages to an employee in component B, creating a shared language between an employee in A and an employee in B. That's one edge. So it's possible.
Thus the algorithm is correct.
Now, implementation details:
We'll read n,m.
Initialize DSU of size n.
We'll have an array for each language (1..m) to store list of employees who know it. Since m <= 100, we can have vector lang[m] (0-index).
For i from 0 to n-1:
read ki.
if ki == 0: maybe we still need to note? Nothing to add.
for j in 0..ki-1: read lang_id; add i to lang[lang_id-1] (since language indices 1..m).
After reading, we union: for each language list, if its size >= 2, then for each employee in that list, union(first_employee, employee).
After DSU built, count number of distinct roots among all employees. We'll also count number of employees that know at least one language: we can just check if total_known > 0 (sum ki). Actually we need to know if any language is known at all. So we can compute total_known while reading.
Then if total_known == 0: print n; else print (components - 1).
Test on some custom cases.
Case: n=4, m=3: emp1: 1; emp2: 2; emp3: 3; emp4: 1 2. Let's compute: emp4 knows 1 and 2. So language1: employees 1,4 -> union(1,4). language2: employees 2,4 -> union(2,4) merges 2 with 1,4. language3: employee3 only. So DSU: component A: {1,2,4}; component B: {3}. total_known >0. components=2. answer=1. Indeed, teach language1 or 2 to employee3 (1 course) connects. Good.
Now, consider if total_known >0 but some employees know multiple languages, etc.
Edge: n=1? But n>=2 per constraints. So fine.
One more check: n=100, m=100, but all employees know at least one language? Then components may be 1 if graph is connected, answer 0.
Now, there might be a subtlety: Could it be that total_known>0 but all employees who know languages are in one component, but there are also isolated employees (zero languages). So components = 1 + number_isolated. Answer = number_isolated. But teaching each isolated employee a language from the main component costs one each, total = number_isolated. That matches.
But is there any scenario where teaching an isolated employee two languages could reduce overall cost? That would increase cost, not reduce.
Thus algorithm is correct.
But I recall that in the known problem, sometimes the answer is components - 1, but they also need to consider if there are employees that know no languages? That is already included because those are separate components.
Now, to be thorough, let's consider if there could be a situation where using an isolated language (none known) could allow connecting two components with 1 edge? No, because if we add an edge from an employee in component A to an isolated language L, that makes L part of component A, but component B remains separate. To connect B, we need an edge from B to L or to A. So two edges needed. So not beneficial.
Thus, final answer.
Now, complexity: O(n*m) or O(n + m + total ki) is fine.
Implementation:
Use DSU class with parent array of size n, and union/find.
Alternatively, we can just use adjacency and BFS on employees using language-employee bipartite graph, but DSU is easiest.
Plan:
Read n,m.
vector<vector> lang_emps(m); // list of employees per language (0-index for employees)
totalLang = 0;
for i in 0..n-1:
read ki; totalLang += ki;
for j in 0..ki-1:
read l; l--;
lang_emps[l].push_back(i);
DSU dsu(n);
for each lang in lang_emps:
if lang.size() > 1:
int first = lang[0];
for emp in lang (starting index 1): dsu.union(first, emp);
Count roots: set roots; for i in 0..n-1: roots.insert(dsu.find(i));
comp = roots.size();
if totalLang == 0: print n; else print comp - 1.
But wait: What about languages with only one employee? They don't create unions, but they still contribute to connectivity? Actually, if a language is known by only one employee, it doesn't directly connect that employee to anyone else. However, that language could be used to connect to others via teaching. But for the initial component detection, an employee who knows only a language that no one else knows is still isolated in the employee graph (since no shared language). However, in the bipartite graph, that employee is connected to that language node, but that component consists of employee + language. In terms of employee connectivity, that employee is a component of size 1 (since no other employees are connected via languages). So indeed, in employee graph, that employee is isolated (since no edge to another employee). So comp count correctly includes it as separate component. The DSU union only happens when multiple employees share a language. So employees who know languages but not shared are not united. So they remain as separate components. That matches the employee graph connectivity.
But is there any scenario where two employees who don't share a language directly could be in the same component via a language that only one of them knows? That doesn't happen because they need an intermediate employee. So they would be separate components until we add edges.
Thus DSU approach works.
But we need to ensure that DSU unions reflect connectivity through languages. However, if we only union employees that directly share a language, that's enough to capture connectivity because if there is a chain A - L1 - B - L2 - C, then A and B share L1, B and C share L2, so A and C are connected via B. The DSU will union A and B due to L1, union B and C due to L2, thus A, B, C all in same set. So it's correct.
Now, we should test with sample 2 using this DSU method:
lang1: employees {1? Actually employees indices: let's index 0..7 for employees 1..8. Emp1 (index0) has 0; Emp2 (index1) knows 1,2,3 => adds to lang0,1,2; Emp3 (index2) knows 1 => lang0; Emp4 (index3) knows 5,4 => lang4,3 (since 5->4? careful: languages 1..7 => indices 0..6). Emp5 (index4) knows 6,7 => lang5,6; Emp6 (index5) knows 3 => lang2; Emp7 (index6) knows 7,4 => lang6,3; Emp8 (index7) knows 1 => lang0.
Now lang0 (language1) has employees: 1,2,7 (indices 1,2,7?) Actually indices: Emp2(1), Emp3(2), Emp8(7) => three employees. So union: (1,2), then (1,7). lang1 (language2): only Emp2 (1) -> nothing. lang2 (language3): employees: Emp2(1), Emp6(5) => union(1,5). lang3 (language4): employees: Emp4(3), Emp7(6) => union(3,6). lang4 (language5): Emp4(3) only. lang5 (language6): Emp5(4) only. lang6 (language7): employees: Emp5(4), Emp7(6) => union(4,6) -> merges component of lang3 (3,6) with 4, so component {3,4,6} etc. So DSU sets: from lang0, we unify {1,2,5,7} (since 1 with 2,7, and later 1 with 5). That's employees indices 1,2,5,7. From lang3 and lang6, we unify {3,4,6}. Employees 0 (Emp1) isolated. So roots: {0}, component for {1,2,5,7}, component for {3,4,6} -> 3 components. totalLang>0 -> answer=2. Good.
Now test with all zero: n=3, m=5, all ki=0. totalLang=0, components: each employee separate, roots size =3. But answer according to formula should be n =3, not comp-1=2. So condition totalLang==0 triggers answer=n. Good.
But consider scenario: n=3, m=5, some employees know languages but the language lists are all empty? That's totalLang=0, answer=3. That's correct because no one knows any language; we need to teach a language to each to connect them. Could we do with 2? Let's test: n=3, teach language1 to employee1 and employee2 (2 edges), now employee1 and employee2 are connected, but employee3 isolated. So need third edge to connect employee3. So yes 3.
Now, what about scenario: totalLang>0 but comp = n (i.e., each employee isolated, but at least one employee knows a language). For that to happen, each employee either knows zero languages or knows languages but not shared with anyone. But if an employee knows a language, that language might be known only by that employee; but that still makes that employee a component with that language, but other employees might be isolated with zero languages. So comp = number of employees (since each is separate). Example: n=3, employee1 knows language1; employees2 and3 know 0. So totalLang=1>0, comp=3. Answer = comp-1 = 2. Is that achievable? Teach employee2 language1 (cost1) -> now employee1 and employee2 share language1, they are connected; employee3 isolated. Then teach employee3 language1 (cost2) -> all connected. So 2 edges. Could we do with 1 edge? If we teach employee2 language1, employee1 and employee2 are connected, but employee3 still isolated. So need at least 2. So answer 2 correct.
Thus the solution is sound.
Now, one more subtlety: The problem says that employees can correspond indirectly via other employees. So if employee A knows language L1, employee B knows L2, and there is an employee C who knows both L1 and L2, then A and B can correspond through C. So connectivity is exactly as described.
Thus final answer: if sum ki == 0 then print n else print (number of DSU components - 1).
But wait, is there any case where sum ki > 0 but number of DSU components - 1 is not enough because some component doesn't have any employee that knows any language? Actually, each component (except isolated zero-knowledge employees) contains at least one language, because if there is an employee that knows a language, that language is in the component. So all components that have at least one employee with a language have languages. Isolated zero-knowledge employees have no languages, but they can be connected with one edge to a component that does. So fine.
Double-check: Could there be a component that has employees but no languages? Suppose an employee knows a language, then that language is in the component, so there is at least one language. So no.
Thus all good.
But one nuance: In the DSU approach, we only consider employees as nodes, and we connect them if they share a language. This effectively counts components as sets of employees that are connected via shared languages. However, what about the case where there is an employee who knows a language but that language is also known by no one else, then that employee is isolated in the employee graph. That's correct. But in the bipartite graph, that employee is connected to that language, so if there is another employee who also knows that language (after we teach), they become connected. So for initial state, that employee is isolated from others. So component count is 1 for that employee. So DSU yields that.
Thus implementation with DSU on employees is fine.
Now, we just need to code.
But before finalizing, let's test on some random cases with brute force small to be absolutely sure. Since n,m ≤ 100, we could also think about corner cases.
Edge case: n=2, m=2, both know different languages: emp1 knows 1, emp2 knows 2. totalLang=2>0, components=2, answer=1. Teach emp1 language2 or emp2 language1. That's 1. Works.
Edge case: n=2, m=2, both know 1 and 2? Then they share languages, components=1, answer=0.
Edge case: n=2, m=2, both know 0? totalLang=0, answer=2. Indeed need to teach a language to both.
Edge case: n=100, m=100, but only one employee knows all languages, others know 0. Then components: one component for the knowledgeable employee (since that employee is connected to many languages, but no other employees share languages). Actually, the knowledgeable employee knows many languages, but others know 0. So each other employee is isolated. So comp = 1 + 99 = 100. totalLang > 0. answer = 99. But can we achieve with 99? Teach each isolated employee one of the languages that the knowledgeable employee knows. That's 99 edges. So yes. Could we do less? Each isolated employee needs at least one edge to connect to the network. So 99 is minimal.
Now, scenario: n=3, m=1, employees: emp1 knows 1, emp2 knows 1, emp3 knows 0. totalLang=2>0, components: emp1 and emp2 share language, so they are in same component; emp3 isolated. comp=2, answer=1. Teach emp3 language1: cost1. Works.
Now, scenario: n=3, m=1, all know 0: totalLang=0, answer=3. But m=1, we can teach language1 to all three: cost3. Could we connect with 2? As argued, need at least 3 because each must learn at least one language. So okay.
Now, consider scenario where there are languages known but none of the employees know any language? That's impossible because if a language is known, some employee knows it. So sum ki>0 ensures at least one employee knows a language.
Thus solution is correct.
I will implement using DSU.
Potential pitfalls:
Input reading: The first number of each line is ki, and then ki integers. For ki=0, there are no integers; we should still read the line correctly. In C++, we can just read ki and then if ki>0 read that many integers.
DSU implementation: path compression and union by rank/size.
Now, before finalizing, let's consider if there's any need to consider the number of languages m? Not really. But there is a corner case: If there is at least one language known, but the only language known is known by all employees? Then comp=1, answer=0.
Now, also consider if totalLang > 0 but all employees who know languages are in one component, but there are also employees who know zero languages; comp = 1 + number_isolated. Answer = number_isolated. That's minimal.
Now, the official solution might be: answer = (number of connected components) - 1 + (if all employees know zero languages then n else 0)? Actually that's what we have.
But I recall from the CF editorial that answer = max( number of connected components - 1, number of employees knowing zero languages )? Let's see: In some solutions, they say: If there is at least one person who knows a language, then answer = number of components - 1; else answer = n. But that's the same because if all know zero, components = n, then components-1 = n-1, but answer should be n, so we need to treat separately. So it's not simply components-1.
Let's test: All know zero: components = n (each isolated). If we output components-1 = n-1, that would be wrong. So must have special case.
Thus correct.
Now, we can also compute answer as: If totalLang == 0 then n else components - 1.
Now, what about case where totalLang > 0 but there are employees who know zero languages, and there is also at least one employee who knows a language. components = 1 + number_isolated? Actually, it could be more if there are multiple language-knowing components. But answer = components - 1. That's correct.
Now, I'll code.
Implementation details in Python (since we can also use Python). The problem constraints are small, so Python is fine.
We'll implement DSU using list parent and size.
Pseudo:
def find(x):
if parent[x]!=x: parent[x]=find(parent[x])
return parent[x]
def union(x,y):
x=find(x); y=find(y)
if x==y: return
if size[x]<size[y]: x,y=y,x
parent[y]=x
size[x]+=size[y]
n,m = map(int,input().split())
lang_emps = [[] for _ in range(m)]
totalLang = 0
for i in range(n):
parts = list(map(int,input().split()))
ki = parts[0]
totalLang += ki
for j in range(1, ki+1):
l = parts[j] - 1
lang_emps[l].append(i)
parent = list(range(n))
size = [1]*n
for lang in lang_emps:
if len(lang) >= 2:
first = lang[0]
for emp in lang[1:]:
union(first, emp)
roots = set()
for i in range(n):
roots.add(find(i))
comp = len(roots)
if totalLang == 0:
print(n)
else:
print(comp - 1)
That should work.
But one more check: In the union loop, we need to union all employees that share a language. However, if there is a language with many employees, union will connect them. This correctly models connectivity.
Now, edge case: employees who know zero languages but are in the same component? That can't happen because they have no languages to connect. So they remain separate.
Now, test with a scenario: n=4, m=3, employees: 1: 1,2; 2: 2; 3: 3; 4: 0. Language1: only employee1; language2: employees1,2 -> union(0,1); language3: employee3 only. So DSU: component A: {0,1}, component B: {2}, component C: {3}. totalLang=4? Actually sum ki= (2+1+1+0)=4>0. comp=3, answer=2. Is it possible with 2? Teach employee3 language2 (cost1) -> merges B with A? Actually employee3 learns language2, now employee3 shares language2 with employee1? Employee1 knows language2, so employee3 becomes connected to employee1, thus merges component B (employee2? Actually employee3 is component B? Wait: after initial, component A has employees 0 and 1; component B has employee 2 (index2) knows language3; component C has employee3 (index3) knows 0. After teaching employee3 (index3) language2, employee3 now knows language2, which is known by employee0 and employee1. So employee3 connects to component A. So now we have component A' = {0,1,3} and component B = {2}. Then teach employee2 (index2) language2 (cost2) -> merges B. So total 2. Works.
Now, could we connect with 1? Teach employee2 (index2) language2 -> then employee2 shares language2 with employee0,1, so merges B with A, but employee3 still isolated. So need at least 2. So answer 2 correct.
Now, what about scenario: employees: 1: 1; 2: 2; 3: 3; 4: 0. totalLang=3, comp=4, answer=3. Teach employee4 language1 (cost1) -> now comp: {0,3? Actually employee1 knows 1, employee4 now knows 1, so they are together; but employee2 and employee3 still separate. So need two more: teach employee2 language1 (cost2) merges, teach employee3 language1 (cost3). That's 3. Could there be a better? Teach language1 to employee2 and employee3 as well? That's 3 edges anyway. So fine.
Now, consider scenario where there is at least one language known, but the DSU components count could be 1 even if there are employees who know zero languages? For DSU components to be 1, all employees must be connected via shared languages. But if an employee knows zero languages, how can it be connected? It can't, because it has no language to share. So DSU component count would be at least 2 if there is an isolated employee with zero languages. So comp-1 includes them.
Thus, answer formula stands.
Now, we can also think of the following: answer = max( number of connected components - 1, number of employees with ki == 0 )? Actually if there are employees with ki==0, they are isolated. But if there is a component with languages, connecting each isolated employee requires one edge, so total edges needed = number of isolated employees + (components among language-knowing employees - 1). But components among language-knowing employees = total components minus isolated employees? Because each isolated employee is its own component. So total components = comp_lang + isolated, where comp_lang is number of components among employees who know at least one language (these components each have at least one language). Then answer = (comp_lang - 1) + isolated = (comp_lang + isolated) - 1 = comp - 1. So same.
Thus, answer = comp - 1 if any employee knows a language, else n.
But wait: in the case where there is at least one language known, comp includes isolated employees. So comp - 1 = number of isolated employees + (comp_lang - 1). That's the number of edges needed. So that's minimal.
Now, verify with scenario where there is a component with languages and many isolated employees. That's correct.
Now, what if there is a component with languages but it's actually not a single component? Actually it's one component.
Now, one more check: Could there be a scenario where there are employees who know languages, but they are all in separate components, and also there are isolated employees. comp = (# of language-knowing components) + (# of isolated employees). answer = comp - 1. That equals (# language components - 1) + (# isolated). That is the sum of edges needed: each isolated employee needs one edge to attach to some language component, and we need to connect the language components among themselves. Could we sometimes do better by using an isolated employee as a bridge? For example, suppose we have two language components A and B, and one isolated employee C. According to formula, we need (2-1)+1 = 2 edges. Could we do with 1 edge? If we teach employee C a language from A and also a language from B? That would be two edges (since teaching two languages costs 2). If we teach C one language from A, then C connects to A; B remains separate. So need one more to connect B, either via C (teach C a language from B) or directly from B to A. So at least 2 edges. So formula holds.
Now, is there any scenario where we could connect multiple components with a single edge by using a language that appears in two components? Initially, a language belongs to only one component. After we add edges, a language could become part of multiple components and thus connect them. But to have a language in two components initially, that's impossible because it's in exactly one component. After adding an edge, language becomes part of one component. To have it connect two components, we need at least two edges: one to bring the language into component A, and another to bring it into component B. So merging two components with no common language requires at least one edge if we use a language that is already in one of them (teach that language to an employee in the other component). That's one edge, which merges them. So for each merge, we can use one edge. So to merge p components, p-1 edges suffice, as long as there's a language in at least one component. That's exactly the situation.
Now, one more nuance: The problem asks: "Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one." This means after teaching, for any two employees, there exists a path of employees where each adjacent pair shares a language. In our solution, we ensure the employee graph is connected. That's sufficient.
Now, we should also consider that the company could teach an employee multiple languages, but that counts as multiple courses. In our edge addition model, each course corresponds to an edge.
Now, the solution seems complete.
Now, I'll produce final answer code in Python.
But before final, let's test some random cases programmatically (mentally) to be absolutely sure.
Case: n=5, m=3. Lists:
Emp1: 1 2
Emp2: 2 3
Emp3: 0
Emp4: 0
Emp5: 3
Now compute:
Language1: emp1 only
Language2: emp1, emp2 -> union(0,1)
Language3: emp2, emp5 -> union(1,4) -> so employees 0,1,4 in one component.
Emp3 and emp4 (index2 and 3) are isolated (zero).
Thus components: comp = 1 (for 0,1,4) + 2 = 3.
totalLang = 2+2+0+0+1 = 5>0.
Answer = 2.
Can we achieve with 2? Option: teach emp3 language2 (cost1) -> connects emp3 to component. Now teach emp4 language2 (cost2) -> connects emp4. So 2 edges. Could we do with 1? If we teach emp3 language2 and emp4 also language2? That would be 2 edges. If we teach emp3 language2 only, emp4 remains isolated. So need at least 2. So answer 2 correct.
Case: n=5, m=3. All employees know only language 1? Actually they all know language1: each list contains 1. Then language1: all 5 employees -> union all. comp=1, answer=0. They can all communicate.
Case: n=5, m=3. Some employees know language1, some language2, and some language3, with no overlap. And there are also isolated employees. For instance: Emp1:1, Emp2:2, Emp3:3, Emp4:0, Emp5:0. Then language1: only emp1; language2: only emp2; language3: only emp3; so no unions. comp=5 (each employee separate). totalLang=3>0, answer=4. Is 4 minimal? Each of the 4 employees (emp2, emp3, emp4, emp5) need to be connected to the network. But we need to connect 5 components. The minimal edges to connect 5 components in a graph where we can add edges between employee and language: we need to use languages as bridges. Since initially each language is tied to one employee, we have three languages each in separate employee components. To connect all, we can choose one language (say language1) and teach it to emp2, emp3, emp4, emp5? That would be 4 edges. That would merge all? Actually emp1 knows language1, so teaching emp2 language1 connects emp2 to emp1 (and to language1). Teaching emp3 language1 connects emp3 as well, etc. So after 4 edges, all employees know language1? Not exactly: we teach each of emp2, emp3, emp4, emp5 language1 (4 courses). Then all employees know language1, so graph is connected. That's 4 edges. Could we do with 3? Let's see if we can connect 5 components with 3 edges. The number of components after adding edges reduces by at most 1 per edge (if each edge connects two different components). To go from 5 to 1, we need at least 4 edges. So lower bound is 4. So answer 4 correct.
Thus answer = comp - 1 is tight when possible (i.e., when each edge can connect two distinct components). In the scenario with all zero languages, comp = n, but can we connect two components with one edge? As argued, to connect two isolated employees (both with no languages), we need at least 2 edges because the edge must be from employee to a language, and to connect them both to the same language we need two edges. So the reduction in number of components per edge is not always 1. In that case, the first edge reduces components? Starting from n isolated employee components and m isolated language components. If we add an edge from an employee to a language, we merge that employee component with that language component. But we still have n-1 employee components isolated. The number of employee components reduces? Actually before adding edge: employee components = n (each employee separate), language components = m (each language separate). After adding edge between employee e and language L: e and L become one component. Employee components? e is no longer isolated; it's now in a component with L. The other employees remain isolated (separate components). So the number of employee components reduces by 1? Because previously e was a component; now e is part of a component that includes L, but still e's component is not an employee component? It still contains an employee, so it's still an employee component. So the number of components containing at least one employee remains the same? Let's compute: Initially, employee components = n (each employee alone). After adding one edge e-L, e is still in a component that contains an employee (e), so that component still counts as an employee component. The other employees are still separate. So the count of employee components is still n? Wait, e's component now includes L, but still has exactly one employee. So number of employee components remains n. So adding an edge between an isolated employee and an isolated language does NOT reduce the number of employee components, because the employee was already a component and still is a component (just now with an extra language). However, the language L becomes no longer isolated, but that doesn't affect employee component count. So to reduce the number of employee components, we need to connect two employee components together. That happens when we add an edge that connects an employee in component A to a language that is already in component B (which contains at least one employee). That merges A and B, reducing employee component count by 1. If there is no component B containing both a language and an employee, i.e., no component with both an employee and a language, then we cannot reduce employee component count with a single edge. In the all-zero case, initially, there are no components that have both an employee and a language; each component is either an employee alone or a language alone. Adding an edge between an employee and a language creates a component that has both an employee and a language, but that doesn't reduce the count of employee components because it merges an employee component with a language component, but we still have the same number of employee components (since that employee was already a component). However, after we have created a component with an employee and a language, we now have a component that contains both an employee and a language. Then adding another edge from another employee to that same language (or to a language in that component) will merge that employee with the component, reducing employee component count by 1. So the first edge does not reduce the count, but subsequent edges that connect other employees to that component do. So to reduce from n employee components to 1, we need at least n edges: first edge creates a component with an employee+language, but employee component count remains n (since that employee was already a component). Then each subsequent edge that connects a new employee to that component reduces count by 1. So total edges needed = n (first edge does not reduce count, but we need n edges to connect all n employees: each employee must be connected to the central component, requiring one edge for the first employee? Let's formalize: Suppose we have n isolated employees and m isolated languages. We want to end with all employees in one component. We can choose a language L. Then we teach L to each employee. That's n edges. After the first edge, we have one employee connected to L, the rest isolated. After all n edges, all employees are connected via L. The number of employee components after each step: initially: n. After first edge: still n (employee1+language is a component, but still one employee component; other n-1 employees are separate; total employee components = 1 + (n-1) = n). After second edge (employee2 learns L): now employee2 joins the component, so employee components = (n-1) because the component now has two employees, and remaining n-2 employees separate -> total employee components = 1 + (n-2) = n-1. So each edge after the first reduces employee components by 1. So total reduction from n to 1 requires (n-1) reductions, which need (n-1) edges, but we also need the first edge to create the component with language? Actually, after the first edge, we still have n components, not reduction. To go from n to 1, we need to reduce by n-1. Since each edge after the first reduces by 1, we need (n-1) such edges, plus the first edge, total n edges. So indeed n edges needed.
Alternatively, if we had started with an existing component that already contains a language and an employee (i.e., some employee already knows a language), then we already have a component with both, so we don't need that first "seed" edge. So then each additional employee can be connected with one edge each, reducing component count by 1 per edge. Starting from comp = number of employee components, and one of them already has a language. So we need (comp - 1) edges.
Thus our condition is exactly: if there is at least one employee who knows a language, then there exists at least one component that contains both an employee and a language (the component of that employee). So we can connect other components with one edge each, reducing count by 1 per edge. Therefore answer = comp - 1.
If no employee knows any language, then there is no component containing both an employee and a language initially. In that case, we first need to create such a component by teaching a language to one employee, costing 1, but that doesn't reduce the number of employee components (still n). Then we need to connect the remaining n-1 employees, each costing 1, reducing components by 1 each. So total n edges. Alternatively, we could create multiple components with languages and then merge them, but still minimum is n. So answer = n.
Hence the reasoning is solid.
Now, implement.
But wait: In the condition totalLang == 0, we print n. However, what if there is at least one language known by some employee, but the component containing that employee does not actually have any language? That's impossible because the employee knows a language, so that language is in the component. So it has a language. So fine.
Now, double-check the condition: totalLang == 0 means no employee knows any language. So answer = n. That's what we have.
But I've seen some solutions that check if there is any language known, they output components-1, else n. That's exactly.
Now, an additional nuance: The DSU approach that connects employees only when they share a language does not account for the fact that an employee who knows a language but no one else shares it is still a component of size 1. So comp includes it. That's correct.
Now, test with random small case using brute force mental: n=2, m=2, emp1: 1, emp2: 2. totalLang=2, comp=2, answer=1. Is there any possible way to connect with 0? No. So correct.
Now, check scenario: n=2, m=2, emp1: 1, emp2: 1 2. Then they share language1, so comp=1, answer=0.
Now, check scenario: n=3, m=2, emp1: 1, emp2: 1, emp3: 2. Then language1: employees 0,1 -> union them. language2: employee2 only. So comp = 2 ( {0,1} and {2} ). totalLang=3>0, answer=1. We can teach employee2 language1 (cost1) and all connected. Could we also teach employee1 language2? That's also 1. So works.
Now, confirm that our DSU correctly counts comp=3: because language1 only emp1, language2 only emp2, no unions. So set roots: 0,1,2.
Now, what about scenario where employees know multiple languages but still no sharing: e.g., emp1: 1,2; emp2: 3,4; etc. No sharing, comp=2. So answer=1.
Now, what about scenario where employees know languages and there is a language known by multiple, but also some employees know zero. Works.
Thus final solution is ready.
Now, provide explanation in the answer.
Now, I'll produce final solution code.
But note: In the problem, languages numbers go from 1 to m. We used zero-index.
One more check: Input reading: each line may have variable length. Using input().split() works fine.
Now, possible pitfalls: The first line contains n and m. Then n lines follow. For each employee line, we need to read all numbers. If ki=0, the line contains only "0". So parts list will be [0]. Then the loop for j in range(1, ki+1) will be range(1,1) => empty, so fine.
Now, implement DSU.
Pseudo-code:
class DSU:
def init(self,n):
self.parent = list(range(n))
self.rank = [0]*n
def find(self,x):
if self.parent[x]!=x:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,x,y):
xr = self.find(x)
yr = self.find(y)
if xr==yr: return
if self.rank[xr] < self.rank[yr]:
xr,yr = yr,xr
self.parent[yr] = xr
if self.rank[xr]==self.rank[yr]:
self.rank[xr] += 1
Now, after reading and unions, compute roots.
But note: we might also consider using a simpler approach: Since n ≤ 100, we could use BFS/DFS on the employee graph. But DSU is fine.
Our code: totalLang = 0+3+1+2+2+1+2+1 = 12>0.
Unions: lang1: employees 1,2,7 -> union(1,2), union(1,7) => {1,2,7}
lang3: employees 1,5 -> union(1,5) => {1,2,5,7}
lang4: employees 3,6 -> union(3,6) => {3,6}
lang7: employees 4,6 -> union(4,6) merges {3,6} with 4 => {3,4,6}
others size 1: no union.
Roots: for employees 0..7, we have: emp0 isolated (0), root for {1,2,5,7} say root1, root for {3,4,6} root3, so 3 roots. answer = 2. Output 2.
Example 3:
2 2
1 2
0
totalLang =1>0. Unions: language2: only employee0? Actually emp1 (index0) knows language2; emp2 (index1) knows 0. No unions because language2 list size=1. So roots: {0}, {1} => comp=2. answer=1. Output 1.
Matches.
Now, we should also test case all zero:
Input:
3 3
0
0
0
Our code: totalLang=0, comp? Unions none, roots: {0},{1},{2} => comp=3. But we will output n=3. So output 3.
Now, consider case where some employees know languages but there is also a language that nobody knows? Not relevant.
Now, potential edge: m could be up to 100, but we only use languages that appear in lists; others are ignored. That's fine.
Now, final solution in Python.
But wait: In DSU, if we union employees based on shared languages, we are essentially building the employee graph connectivity. However, there is a subtle case: Could two employees be connected via a language even if they don't directly share a language but there is a chain of languages through other employees? That's captured by DSU because if A shares L1 with B, and B shares L2 with C, then A and C are in the same set because unions will unify A with B (via L1) and B with C (via L2), thus A, B, C all in one set. So DSU correctly models connectivity.
Thus final.
Now, we may also simplify: Instead of DSU, we could use BFS on the employee graph constructed by adjacency. But DSU is simple.
Now, one more check: The problem states that the company pays for lessons; each course costs 1 berdollar. They can learn any number of languages. Our solution adds edges which correspond to courses. But we should ensure that we are not double-counting something: If we connect components by teaching a language to an employee, that's one course. That's exactly what we count. Good.
Now, confirm with official note: In second sample, they said employee 1 can learn language 2, and employee 8 can learn language 4. That's 2 courses. Our answer matches.
Now, one might ask: Could there be a scenario where teaching a language to two employees is not the best because maybe teaching a language to one employee and another language to another could be cheaper? But our formula gives minimal.
Thus, answer ready.
Now, write final explanation in the answer section. Provide code.
But the user asked: "The 'BerCorp' company has got n employees... Find the minimum sum of money..." So we need to output the answer.
We'll provide solution in Python.
But also note that sometimes in Codeforces problem, they use input reading that might be faster; but with small constraints, it's fine.
Now, I'll produce final message. ```python
import sys
class DSU:
def init(self, n):
self.parent = list(range(n))
self.rank = [0] * n
code
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, a, b):
ra = self.find(a)
rb = self.find(b)
if ra == rb:
return
if self.rank[ra] < self.rank[rb]:
ra, rb = rb, ra
self.parent[rb] = ra
if self.rank[ra] == self.rank[rb]:
self.rank[ra] += 1
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
code
# list of employees knowing each language (0‑based indices)
lang_emp = [[] for _ in range(m)]
total_known = 0
for emp in range(n):
k = int(next(it))
total_known += k
for _ in range(k):
l = int(next(it)) - 1 # language 0..m-1
lang_emp[l].append(emp)
dsu = DSU(n)
# unite employees that share a language
for lst in lang_emp:
if len(lst) >= 2:
first = lst[0]
for emp in lst[1:]:
dsu.union(first, emp)
# count distinct components that contain at least one employee
roots = set()
for i in range(n):
roots.add(dsu.find(i))
comp = len(roots)
if total_known == 0:
print(n)
else:
print(comp - 1)