This page is still under construction.

Parts of this page are still being built. What you see may change.

Learning Languages

Time limit1sMemory limit128 MB

Summary
Given which languages each cow speaks, find how many extra language lessons are needed so that every pair of cows is connected through shared languages.
Level

Medium6 of 10

Topics
Graph, Union-find, Hash map, Implementation
Solved
No attempts yet

Problem

Farmer John's NN (2≤N≤10,0002 \le N \le 10{,}000) cows, conveniently numbered 1…N1 \ldots N, are fluent in some MM (1≤M≤30,0001 \le M \le 30{,}000) languages, also conveniently numbered 1…M1 \ldots M. Cow ii can speak KiK_i (1≤Ki≤M1 \le K_i \le M) languages, namely Li1,Li2,…,LiKiL_{i1}, L_{i2}, \ldots, L_{iK_i} (1≤Lij≤M1 \le L_{ij} \le M). FJ's cows aren't THAT smart, so the sum of KiK_i over all cows ii is at most 100,000100{,}000.

Two cows can't directly talk to each other unless both speak a common language. However, cows can pass messages along, translating if necessary. In other words, cows AA and BB can have a conversation if and only if there exists a sequence of cows T1,T2,…,TkT_1, T_2, \ldots, T_k such that AA and T1T_1 share a language, T1T_1 and T2T_2 share a language, and so on, and TkT_k and BB share a language.

Farmer John wishes that his cows could be even more social, so he wants every cow to be able to socialize with any other cow. He can buy books to teach any one of his cows any language he pleases. Being a fairly frugal farmer, FJ wants to purchase the minimum number of books necessary to enable all of his cows to speak to each other. Help him determine that minimum number of books.

By way of example, suppose there are three cows named Alberta, Bessie, and Contessa along with three languages denoted #1, #2, and #3. Alberta can speak languages #2 and #3, Bessie can speak language #2, and Contessa can speak language #1. Currently, Alberta and Bessie can talk to each other, but Contessa is left alone.

             #1  #2  #3
Alberta           x   x
Bessie            x
Contessa      x

FJ can buy Contessa a book to teach her language #2, after which all three cows share language #2 and can communicate. (Teaching her language #3 would also work, since she could then reach Bessie through Alberta.) Either way, exactly one book is required here.

Input

  • Line 1: Two space-separated integers, NN and MM.
  • Lines 2…N+12 \ldots N+1: Line i+1i+1 describes the languages cow ii speaks, as Ki+1K_i + 1 space-separated integers: Ki,Li1,Li2,…,LiKiK_i, L_{i1}, L_{i2}, \ldots, L_{iK_i}.

Output

  • A single integer: the minimum number of books FJ must purchase so that every cow can communicate (directly or indirectly) with every other cow.

Examples5

  1. Example 1

    Input
    3 3
    2 3 2
    1 2
    1 1
    
    Expected output
    1
    
  2. Example 2

    Input
    2 1
    1 1
    1 1
    
    Expected output
    0
    
  3. Example 3

    Input
    2 2
    1 1
    1 2
    
    Expected output
    1
    
  4. Example 4

    Input
    4 4
    2 1 2
    2 2 3
    2 3 4
    1 4
    
    Expected output
    0
    
  5. Example 5

    Input
    5 5
    1 1
    1 2
    1 3
    1 4
    1 5
    
    Expected output
    4