Computer Programming Contest Preparation

ToolBox - Source for: 109/10926/b.c



/home/toolbox/public_html/solutions/109/10926/b.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2018-03-18
   18  * Purpose: fun
   19  * Problem: 10926 - How Many Dependencies?
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define TASK_LIMIT 102
   27 
   28 int numTasks;
   29 int tasks[TASK_LIMIT][TASK_LIMIT];
   30 
   31 void init(int x)
   32 {
   33     /* FUNCTION init */
   34     int i;
   35     int j;
   36 
   37     for (i=0; x>i; i++)
   38         {
   39             /* for each row */
   40             for (j=0; x>j; j++)
   41                 {
   42                     /* for each column */
   43                     tasks[i][j] = 0;
   44                 } /* for each column */
   45         } /* for each row */
   46 } /* FUNCTION init */
   47 
   48 void dump()
   49 {
   50     /* FUNCTION dump */
   51 } /* FUNCTION dump */
   52 
   53 int getInput()
   54 {
   55     /* FUNCTION getInput */
   56     int dataReadFlag;
   57     int i;
   58     int j;
   59     int cnt;
   60     int dep;
   61 
   62     scanf(" %d ", &numTasks);
   63     init(numTasks);
   64     for (i=1; numTasks>=i; i++)
   65         {
   66             /* for each task */
   67             scanf(" %d ", &cnt);
   68             tasks[i][0] = 0;
   69             for (j=1; cnt>=j; j++)
   70                 {
   71                     /* for each dependency */
   72                     scanf(" %d ", &dep);
   73                     tasks[i][dep] = 1;
   74                 } /* for each dependency */
   75         } /* for each task */
   76     dataReadFlag = (0 < numTasks);
   77     return (dataReadFlag);
   78 } /* FUNCTION getInput */
   79 
   80 void sum()
   81 {
   82     /* FUNCTION sum */
   83     int i;
   84     int j;
   85 
   86     for (i=1; numTasks>=i; i++)
   87         {
   88             /* for each task */
   89             for (j=1; numTasks>=j; j++)
   90                 {
   91                     /* for each possible dependency */
   92                     tasks[i][0] = tasks[i][0] + tasks[i][j];
   93                 } /* for each possible dependency */
   94         } /* for each task */
   95 } /* FUNCTION sum */
   96 
   97 void process()
   98 {
   99     /* FUNCTION process */
  100     int i;
  101     int j;
  102     int mx = 0;
  103 
  104     sum();
  105     for (i=1; numTasks>i; i++)
  106         {
  107             /* for each task */
  108             mx = (mx > tasks[i][0]) ? mx : tasks[i][0];
  109         } /* for each task */
  110     for (i=1; numTasks>=i; i++)
  111         {
  112             /* for each task */
  113             if (mx == tasks[i][0])
  114                 {
  115                     /* find first max */
  116                     printf("%d\n", i);
  117                     numTasks = 0;
  118                 } /* find first max */
  119         } /* for each task */
  120 
  121 
  122 } /* FUNCTION process */
  123 
  124 int main()
  125 {
  126     /* main */
  127     int moreToDo;
  128 
  129     moreToDo = getInput();
  130     while (moreToDo)
  131         {
  132             /* while */
  133             process();
  134             moreToDo = getInput();
  135         } /* while */
  136 
  137     return EXIT_SUCCESS;
  138 } /* main */
  139