Computer Programming Contest Preparation

ToolBox - Source for: 11/1197/a.c



/home/toolbox/public_html/solutions/11/1197/a.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: 1197 - The Suspects
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_STUDENTS 30002
   27 
   28 int numStudents;
   29 int numGroups;
   30 int students[MAX_STUDENTS];
   31 
   32 void init(int num)
   33 {
   34     /* FUNCTION init */
   35     int i;
   36 
   37     for (i=0; num>i; i++)
   38         {
   39             /* for each student */
   40             students[i] = i;
   41         } /* for each student */
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 void reset(int hunt, int replace)
   50 {
   51     /* FUNCTION reset */
   52     int i;
   53 
   54 
   55     DEBUG printf("Change %d to %d: ", hunt, replace);
   56     for (i=0; numStudents>i; i++)
   57         {
   58             /* for each student */
   59             if (students[i] == hunt)
   60                 {
   61                     /* value found */
   62                     students[i] = replace;
   63                     DEBUG printf(".");
   64                 } /* value found */
   65         } /* for each student */
   66     DEBUG printf("\n");
   67 } /* FUNCTION reset */
   68 
   69 int getInput()
   70 {
   71     /* FUNCTION getInput */
   72     int dataReadFlag;
   73     int i;
   74     int j;
   75     int k;
   76     int groupCnt;
   77     int first;
   78     int grp;
   79 
   80     scanf(" %d %d ", &numStudents, &numGroups);
   81     dataReadFlag = ((0 != numStudents) || (0 != numGroups));
   82     DEBUG printf("numStudents = %d    numGroups = %d   dataReadFlag = %d\n", numStudents, numGroups, dataReadFlag);
   83     if (dataReadFlag)
   84         {
   85             /* load case data */
   86             init(numStudents);
   87             for (i=0; numGroups>i; i++)
   88                 {
   89                     /* for each group */
   90                     scanf(" %d ", &groupCnt);
   91                     if (0 < groupCnt)
   92                         {
   93                             /* group has at least one member */
   94                             scanf(" %d ", &first);
   95                             first = students[first];
   96                             for (k=1; groupCnt>k; k++)
   97                                 {
   98                                     /* for each group member */
   99                                     scanf(" %d ", &grp);
  100                                     grp = students[grp];
  101                                     if (0 == grp)
  102                                         {
  103                                             /* keep 0, reset other */
  104                                             grp = first;
  105                                             first = 0;
  106                                         } /* keep 0, reset other */
  107                                     if (grp != first)
  108                                         {
  109                                             reset(grp, first);
  110                                         }
  111                                 } /* for each group member */
  112                         } /* group has at least one member */
  113                 } /* for each group */
  114 
  115         } /* load case data */
  116 
  117     return (dataReadFlag);
  118 } /* FUNCTION getInput */
  119 
  120 void process()
  121 {
  122     /* FUNCTION process */
  123     int i;
  124     int cnt = 0;
  125 
  126     for (i=0; numStudents>i; i++)
  127         {
  128             /* for each student */
  129             if (0 == students[i])
  130                 {
  131                     cnt++;
  132                 }
  133         } /* for each student */
  134     printf("%d\n", cnt);
  135 } /* FUNCTION process */
  136 
  137 int main()
  138 {
  139     /* main */
  140     int moreToDo;
  141 
  142     moreToDo = getInput();
  143     while (moreToDo)
  144         {
  145             /* while */
  146             DEBUG printf("CASE\n");
  147             process();
  148             moreToDo = getInput();
  149         } /* while */
  150 
  151     return EXIT_SUCCESS;
  152 } /* main */
  153