DISHLIFE - Editorial

PROBLEM LINK

Practice
Contest

DIFFICULTY

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.

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

Plz see my code as ā€¦all the public cases are correct and getting wrong ansā€¦
plz tell where you are wrongā€¦
Link

Links to setterā€™s solution and testerā€™s solution are not working, please fix this.

I request your scrutiny on this code CodeChef: Practical coding for everyone . 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.

1 Like

there is something wrong in the last statement I see ā€œTime complexity of this algorithm will be equal to $\text{ingredient}ā€¦ā€
Also the setters solution and testers solution are not working, please fix it

If the input is-

1
3 3
2 1 2
1 3
2 1 3

or if it is

1
3 3
2 1 3
1 3
2 1 2

The answer in both the cases should be ā€œsomeā€. Because for the 1st case we can skip 2nd or 3rd island and for the 2nd case we can skip 1st or 2nd island.
But the judge is accepting solutions in which for the first case o/p is ā€œsomeā€ and for the second case o/p is ā€œallā€.
This is the case if someone is starting from the first island and iterating over all the island one by one and checking if at any island(not the last one) all the ingredients are covered or not. But this is the wrong approach.

#include <stdio.h>

int main()
{

int t,n,k,i,j,a[100000],b[100000][5],ch;

scanf("%d",&t);

while(t--)

{

    scanf("%d%d",&n,&k);

    for(i=0;i<k;i++)

    a[i]=0;

    ch=0;

    for(i=0;i<n;i++)

    {

        scanf("%d",&b[i][0]);

        for(j=1;j<=b[i][0];j++)

        {

            scanf("%d",&b[i][j]);

            a[b[i][j]-1]++;

        }

    }

    for(i=0;i<k;i++)

    if(a[i]==0)

    {
        ch=1;break;//impossible

    }

    if(ch==1)

    printf("sad\n");

    else//possible

    {
        for(i=0;i<n;i++)

        {
            ch=0;

            for(j=1;j<=b[i][0];j++)

          {

            if(a[b[i][j]-1]==1)//its contri matters

            {
                ch=1;break;

            }

          }

        if(ch==0)//its contri doesn't matter

        {

            printf("some\n");

            break;
        }

        }

        if(ch==1)

        printf("all\n");

    }
    
}
return 0;

}
I dont understand why this code works(100 marks) bcoz no. of columns in array b[100000][5] which is used to

store ingredients of a particular island is of size 5 only still code works for original constraints?Why plz

explain.

In my solution 2nd test case getting failed because of TLE. CodeChef: Practical coding for everyone
Please anyone let me know, how I can improve. Thanks

i solved this question simply by using set.
my solution CodeChef: Practical coding for everyone

1 Like

for all the newbies , this one solution of mine will certainly help , follow it line by line ā€¦ CodeChef: Practical coding for everyone

3 Likes

I think the judge for this problem is incorrect.

Consider the following input:

1

2 3

2 1 3

2 1 2

The answer should be ā€˜allā€™.

My solution which prints ā€œallā€

Another solution which prints ā€œsomeā€

Both solutions were passed by the judge giving both the solutions 100 points.

can anyone tell me whats wrong wid my codeā€¦
link : CodeChef: Practical coding for everyone
i have done using adjecancy matrix !!

I dont have enough karma to ask a question, thatā€™s why i am posting my code here. i donā€™t know whats wrong with this,please help!!!

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(tā€“)
{
int n,k;
cin>>n>>k;
int p,ele,ele_count=0;
int i,flag2=0;

	int hash[100001]={0};

for(i=0; i<n; i++)
{
	
	cin>>p;
	
	
	for(int j=0; j<p; j++)
	{
		cin>>ele;
		
		if(hash[ele]!=1)
		{
			hash[ele]=1;
			ele_count++;
		   }
		  
		  
	   }
	    if(ele_count==k)
	    {
	    	if(i+1<n) flag2=1;
		}

   }
   
   if(ele_count!=k) cout<<"sad"<<endl;
   else
   {
   	if(flag2==1) cout<<"some"<<endl;
   	else cout<<"all"<<endl;
   }

}
}

The size of the 2-D array should be n * k. No? But when I take a[n][k], I get wrong answer and runtime error in the last 2 test cases. when I take the size n * 1000, the code works totally fine. Also, I saw a code where even n*20 was fine enough to get all AC.

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ā€.

Hereā€™s, my very similar solution: CodeChef: Practical coding for everyone

Great, I understood. Thank you for your help!

Reason you are getting run time error is that you have created matrix[n][k] and you are accessing matrix[n][k] while the max you can access is matrix[n-1][k-1]. As far as TLE is concerned I think your solution is O(N^3).