len(speaks) members, numbered from 1. There are language_count languages, also numbered from 1. The list speaks[i] holds the languages that member i + 1 knows. Each pair [u, v] in pairs means members u and v are friends. Two friends can talk if they share at least one language.
You pick **one** language and teach it to some members so that afterwards every pair of friends can talk. Return the fewest members you have to teach.
Friendship is not transitive: if u is friends with v and v with w, that does not make u and w friends.language_count=3, speaks=[[1], [2, 3], [3]], pairs=[[1, 2], [2, 3], [1, 3]] → 1
Pairs 1–2 and 1–3 cannot talk. Teaching person 1 language 3 is enough, since persons 2 and 3 both know it.
language_count=2, speaks=[[1], [1, 2], [2]], pairs=[[1, 2], [2, 3]] → 0
Every pair already shares a language, so nobody needs lessons.
language_count ≤ 500language_count; the languages in speaks[i] are distinct and between 1 and language_countlanguage_count=3, speaks=[[1], [2, 3], [3]], pairs=[[1, 2], [2, 3], [1, 3]]
1
Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.