Answers to: DISHLIFE - Editorialhttps://discuss.codechef.com/questions/95658/dishlife-editorial<h1>PROBLEM LINK</h1>
<p><a href="http://www.codechef.com/problems/DISHLIFE">Practice</a><br>
<a href="http://www.codechef.com/APRIL17/problems/DISHLIFE">Contest</a></p>
<h1>DIFFICULTY</h1>
<p>simple</p>
<h1>PREREQUISITES</h1>
<p>general programming skills</p>
<h1>PROBLEM</h1>
<p>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.</p>
<ul>
<li>Whether it is even possible to collect the $k$ required ingredients. <ul>
<li>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. </li>
</ul>
</li>
</ul>
<p>You have to identify which of these scenarios is there.</p>
<hr>
<p>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. </p>
<p>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. </p>
<p>We can check whether there is some ingredient whose $cnt$ value is zero or not.</p>
<pre><code>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.
</code></pre>
<hr>
<p>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. </p>
<pre><code>// 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;
</code></pre>
<p>Time complexity of this algorithm will be equal to $\text{ingredient}<a href="http://www.codechef.com/problems/DISHLIFE">1</a>.\text{size}() \, + \, \text{ingredient}<a href="http://www.codechef.com/APRIL17/problems/DISHLIFE">2</a>.\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.</p>
<h1>SETTER'S SOLUTION</h1>
<p>Can be found <a href="http://www.codechef.com/download/Solutions/APRIL17/Setter/DISHLIFE.cpp">here</a>.</p>
<h1>TESTER'S SOLUTION</h1>
<p>Can be found <a href="http://www.codechef.com/download/Solutions/APRIL17/Tester/DISHLIFE.cpp">here</a>.</p>enMon, 02 Jul 2018 23:03:44 +0530Answer by utkarsh911https://discuss.codechef.com/questions/95658/dishlife-editorial/130775<p>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.</p>utkarsh911Mon, 02 Jul 2018 23:03:44 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/130775Answer by kamalneelhttps://discuss.codechef.com/questions/95658/dishlife-editorial/105238<p>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!!!</p>
<h1>include&lt;bits stdc++.h=""&gt;</h1>
<p>using namespace std;</p>
<p>int main()
{
int t;
cin&gt;&gt;t;
while(t--)
{
int n,k;
cin&gt;&gt;n&gt;&gt;k;
int p,ele,ele_count=0;
int i,flag2=0;</p>
<pre><code> int hash[100001]={0};
for(i=0; i&lt;n; i++)
{
cin&gt;&gt;p;
for(int j=0; j&lt;p; j++)
{
cin&gt;&gt;ele;
if(hash[ele]!=1)
{
hash[ele]=1;
ele_count++;
}
}
if(ele_count==k)
{
if(i+1&lt;n) flag2=1;
}
}
if(ele_count!=k) cout&lt;&lt;"sad"&lt;&lt;endl;
else
{
if(flag2==1) cout&lt;&lt;"some"&lt;&lt;endl;
else cout&lt;&lt;"all"&lt;&lt;endl;
}
</code></pre>
<p>}
}</p>kamalneelSat, 15 Jul 2017 23:48:02 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/105238Comment by siddharthp538 on msd_007's answerhttps://discuss.codechef.com/questions/95658/dishlife-editorial#101169<p>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).</p>siddharthp538Mon, 12 Jun 2017 14:21:59 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial#101169Answer by msd_007https://discuss.codechef.com/questions/95658/dishlife-editorial/101162<p>can anyone tell me whats wrong wid my code.....
link : <a href="https://www.codechef.com/viewsolution/14232897">https://www.codechef.com/viewsolution/14232897</a>
i have done using adjecancy matrix !!</p>msd_007Mon, 12 Jun 2017 11:50:06 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/101162Answer by kay_kayhttps://discuss.codechef.com/questions/95658/dishlife-editorial/97849<p><strong>I think the judge for this problem is incorrect.</strong></p>
<p>Consider the following input:</p>
<p>1</p>
<p>2 3</p>
<p>2 1 3</p>
<p>2 1 2</p>
<p>The answer should be <strong>'all'</strong>.</p>
<p><a href="https://www.codechef.com/viewsolution/13520009">My solution which prints "all"</a></p>
<p><a href="https://www.codechef.com/viewsolution/13357983">Another solution which prints "some"</a></p>
<p>Both solutions were passed by the judge giving both the solutions 100 points.</p>kay_kayWed, 10 May 2017 11:30:19 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/97849Answer by marshal_roxxhttps://discuss.codechef.com/questions/95658/dishlife-editorial/96159<p>for all the newbies , this one solution of mine will certainly help , follow it line by line .... <a href="https://www.codechef.com/viewsolution/13277031">https://www.codechef.com/viewsolution/13277031</a></p>marshal_roxxThu, 20 Apr 2017 21:29:36 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/96159Answer by pk201996https://discuss.codechef.com/questions/95658/dishlife-editorial/96151<p>i solved this question simply by using set.
my solution <a href="https://www.codechef.com/viewsolution/13233163">https://www.codechef.com/viewsolution/13233163</a></p>pk201996Thu, 20 Apr 2017 20:54:00 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/96151Answer by mahinlmanhttps://discuss.codechef.com/questions/95658/dishlife-editorial/96130<p>In my solution 2nd test case getting failed because of TLE. <a href="https://www.codechef.com/viewsolution/13274266">https://www.codechef.com/viewsolution/13274266</a>
Please anyone let me know, how I can improve. Thanks</p>mahinlmanThu, 20 Apr 2017 18:37:10 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/96130Answer by code_manhttps://discuss.codechef.com/questions/95658/dishlife-editorial/96015<h1>include &lt;stdio.h&gt;</h1>
<p>int main()
{</p>
<pre><code>int t,n,k,i,j,a[100000],b[100000][5],ch;
scanf("%d",&amp;t);
while(t--)
{
scanf("%d%d",&amp;n,&amp;k);
for(i=0;i&lt;k;i++)
a[i]=0;
ch=0;
for(i=0;i&lt;n;i++)
{
scanf("%d",&amp;b[i][0]);
for(j=1;j&lt;=b[i][0];j++)
{
scanf("%d",&amp;b[i][j]);
a[b[i][j]-1]++;
}
}
for(i=0;i&lt;k;i++)
if(a[i]==0)
{
ch=1;break;//impossible
}
if(ch==1)
printf("sad\n");
else//possible
{
for(i=0;i&lt;n;i++)
{
ch=0;
for(j=1;j&lt;=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;
</code></pre>
<p>}
I dont understand why this code works(100 marks) bcoz no. of columns in array b[100000][5] which is used to</p>
<p>store ingredients of a particular island is of size 5 only still code works for original constraints?Why plz</p>
<p>explain. </p>code_manWed, 19 Apr 2017 12:30:52 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/96015Answer by sajalagrawalhttps://discuss.codechef.com/questions/95658/dishlife-editorial/95960<p>If the input is-</p>
<pre><code>1
3 3
2 1 2
1 3
2 1 3
</code></pre>
<p>or if it is</p>
<pre><code>1
3 3
2 1 3
1 3
2 1 2
</code></pre>
<p>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.</p>sajalagrawalTue, 18 Apr 2017 20:50:33 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/95960Answer by amveghttps://discuss.codechef.com/questions/95658/dishlife-editorial/95901<p>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</p>amvegTue, 18 Apr 2017 09:34:32 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/95901Answer by henrykibiwotthttps://discuss.codechef.com/questions/95658/dishlife-editorial/95884<p>I request your scrutiny on this code <a href="https://www.codechef.com/viewsolution/13350172">https://www.codechef.com/viewsolution/13350172</a> . 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.</p>henrykibiwottTue, 18 Apr 2017 00:56:50 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/95884Answer by only4https://discuss.codechef.com/questions/95658/dishlife-editorial/95849<p>Links to setter's solution and tester's solution are not working, please fix this.</p>only4Mon, 17 Apr 2017 21:20:49 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/95849Comment by sharpcoderr on sharpcoderr's answerhttps://discuss.codechef.com/questions/95658/dishlife-editorial#95828<p>Great, I understood. Thank you for your help!</p>sharpcoderrMon, 17 Apr 2017 20:23:08 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial#95828Comment by pratik_gadhiya on sharpcoderr's answerhttps://discuss.codechef.com/questions/95658/dishlife-editorial#95796<p>Here's, my very similar solution: <a href="https://www.codechef.com/viewsolution/13218833">https://www.codechef.com/viewsolution/13218833</a></p>pratik_gadhiyaMon, 17 Apr 2017 18:55:36 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial#95796Comment by pratik_gadhiya on sharpcoderr's answerhttps://discuss.codechef.com/questions/95658/dishlife-editorial#95794<p>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".</p>pratik_gadhiyaMon, 17 Apr 2017 18:53:24 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial#95794Answer by palash7563https://discuss.codechef.com/questions/95658/dishlife-editorial/95793<p>Plz see my code as ...all the public cases are correct and getting wrong ans..
plz tell where you are wrong....
<a href="https://www.codechef.com/viewsolution/13297216">Link</a></p>palash7563Mon, 17 Apr 2017 18:47:18 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/95793Answer by sharpcoderrhttps://discuss.codechef.com/questions/95658/dishlife-editorial/95757<p>Hi, Can anyone please explain why I'm getting the last 2 cases wrong?</p>
<p>I implemented the same logic as in the editorial.</p>
<p><a href="https://www.codechef.com/viewsolution/13350978">https://www.codechef.com/viewsolution/13350978</a></p>sharpcoderrMon, 17 Apr 2017 16:52:45 +0530https://discuss.codechef.com/questions/95658/dishlife-editorial/95757