Question: Question is Nom the Panda is back with another one of his peanut butter adventures! He s got a list of peanut butter treats to
Question is
Nom the Panda is back with another one of his peanut butter adventures! Hes got a list of peanut butter treats to make, but they have to be done in a particular order. Some treats depend on others, which means Nom cant start a new treat until all its ingredients or prep steps are ready.
Noms goal is to whip up all these treats as quickly as possible, grouping together as many treats as he can in each round of cooking, as long as none of them depend on each other. Every round is a time slot where Nom can work on multiple treats that dont require waiting on any other treats.
Help Nom by figuring out two things:
A good order for preparing all his treats, making sure he follows the steps in the right order.
The minimum number of rounds Nom needs to finish everything by grouping treats that can be prepared together.
Can you help Nom complete his peanut butter extravaganza in record time?
Constraints:
N : Number of peanut butter treats Nom wants to make.
N : Number of dependencies between treats.
For each dependency pair ab treat b depends on treat a meaning Nom cant start on treat b until he finishes treat a
Each treat is labeled from to N
Input from atxt:
The first line contains two integers N and M: the number of treats Nom wants to make and the number of dependencies between them.
The next M lines each contain two integers a and b representing a dependency where treat b depends on treat a
Output to btxt:
Print a single integer x the minimum number of rounds Nom needs to complete all his treats
Follow this with x lines, where each line represents a round. Each line contains the treat numbers in ascending order that Nom can prepare in that round.
Sample Case :
Input atxt:
Output btxt:
Sample Case :
Input atxt:
Output btxt:
so i have written code in c language for the above question i took of chatgpt and used my logic to write the code so go through the code make it humanize it with same logic so here is the c language code
#include
#include
#define MAX
Function to calculate the sum of dependency pairs not used in main
int sumConstraintNomPbint dependencies int M
int sum ;
for int i ; i M; i
sum dependenciesi dependenciesi;
return sum;
Comparison function for qsort to sort integers in ascending order
int compareconst void a const void b
return int a int b;
Topological Sort to determine order and group into rounds
void topologicalSortint N int adjMAXMAX int inDegreeMAX FILE outputFile
int queueMAX front rear ;
int rounds ;
for int i ; i N; i
if inDegreei
queuerear i;
while front rear
int roundSize rear front;
int tempMAX tempSize ;
rounds;
for int i ; i roundSize; i
int curr queuefront;
temptempSize curr;
for int j ; j N; j
if adjcurrj
inDegreej;
if inDegreej
queuerear j;
Sort and output treats in this round
qsorttemp tempSize, sizeofint compare;
for int i ; i tempSize; i
fprintfoutputFiled tempi;
fprintfoutputFile
;
Write number of rounds at the top of the output
fseekoutputFile SEEKSET;
fprintfoutputFiled
rounds;
int main
FILE inputFile fopenatxtr;
FILE outputFile fopenbtxtw;
if inputFile outputFile
printfError opening file.
;
return ;
int N M;
fscanfinputFiled d &N &M;
int adjMAXMAX;
int inDegreeMAX;
int dependenciesM;
for int i ; i M; i
int a b;
fscanfinputFiled d &a &b;
adjab;
inDegreeb;
dependenciesi a;
dependenciesi b;
fprintfoutputFile
; Placeholder for number of rounds
topologicalSortN adj, inDegree, outputFile;
fcloseinputFile;
fcloseoutputFile;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
