×

simple

# PREREQUISITES

general programming skills

# PROBLEM

There are $n$ islands, and $k$ ingredients numbered from $1$ to $k$. Each island contains some ingredients, let ${ingredient}_i$ denotes the list of ingredients in $i$-th island. Chef wants to collect these ingredients from these islands. He wants to check following cases.

• Whether it is even possible to collect the $k$ required ingredients.
• If yes, then he wants to know whether he will need to visit all the $n$ islands for collecting these ingredients, or he can do it by visiting less than $n$ islands.

You have to identify which of these scenarios is there.

Checking whether Chef can even collect the desired $k$ ingredients or not. This is same as checking whether is there some ingredient which is not present in any island.

For each ingredient, we can maintain the number of islands it is present in. let $cnt[i]$ denote the number of islands in which the ingredient $i$ is present.

We can check whether there is some ingredient whose $cnt$ value is zero or not.

int cnt[K + 1]
for i = 1 to n:
for j = 0 to ingredients[i].size():
x = ingredients[i][j]
cnt[x] += 1;
for i = 1 to k:
if (cnt[i] == 0):
// It means that ingredient i is not present in any of the islands.


Now, we know that it is possible to collect the $k$ ingredient. Now we should find whether Chef will need to visit all the $n$ islands for collecting these ingredients or not. If there is a ingredient $i$ which is present only in a single island, i.e. $cnt[i] = 1$, then you will have to definitely need to visit this island. Otherwise, you can skip this island, and collect the ingredients from remaining $n - 1$ islands.

// For each island, check if Chef doesn't visit this island, can he still visit collect all the ingredients from the remaining islands?
need_to_visit_all = true;
for i = 1 to n:
can_collect_all_ingredient_without_this_island = true;
for j = 0 to ingredients[i].size():
x = ingredients[i][j]
if (cnt[x] == 1):
can_collect_all_ingredient_without_this_island = false
if (can_collect_all_ingredient_without_this_island):
need_to_visit_all = false;


Time complexity of this algorithm will be equal to $\text{ingredient}1.\text{size}() \, + \, \text{ingredient}2.\text{size}() + \dots +\text{ingredient}[n].\text{size}()$. For solving the final subtask, we have the constraints over sum that it will not exceed $10^6$. Hence, it will take around $10^6$ operations for answering each test case.

# SETTER'S SOLUTION

Can be found here.

# TESTER'S SOLUTION

Can be found here.

This question is marked "community wiki".

2.2k52122159
accept rate: 21%

 1 I request your scrutiny on this code https://www.codechef.com/viewsolution/13350172 . I would like to know areas it could be optimized in order for it to run faster and also to rid off SIGSEGV. The code runs well with the provided test cases but it fails the online judge test cases. answered 18 Apr, 00:56 11●1 accept rate: 0%
 1 i solved this question simply by using set. my solution https://www.codechef.com/viewsolution/13233163 answered 20 Apr, 20:54 3★pk201996 11●1 accept rate: 0%
 0 Hi, Can anyone please explain why I'm getting the last 2 cases wrong? I implemented the same logic as in the editorial. https://www.codechef.com/viewsolution/13350978 answered 17 Apr, 16:52 1 accept rate: 0% While checking; if (cnt[islands[i][j]] == 1), add a break condition inside the if condition. Your solution woundn't work for this test case; n = 3, k = 4, {1,4}, {2,3}, {4}. In your case, answer would be "all" since v=3 (v==n). But, clearly it should be "some". (17 Apr, 18:53) Here's, my very similar solution: https://www.codechef.com/viewsolution/13218833 (17 Apr, 18:55) Great, I understood. Thank you for your help! (17 Apr, 20:23)
 0 Plz see my code as ...all the public cases are correct and getting wrong ans.. plz tell where you are wrong.... Link answered 17 Apr, 18:47 15●3 accept rate: 0%
 0 Links to setter's solution and tester's solution are not working, please fix this. answered 17 Apr, 21:20 4★only4 1.5k●1●9 accept rate: 17%

Question tags:

×10,847
×122
×5