Computer Programming Contest Preparation

ToolBox - Source for: 7/793/a.c



/home/toolbox/public_html/solutions/7/793/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2016-09-08
   20  * Purpose: fun
   21  * Problem: 793 - Network Connections
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_LINE 1024
   29 
   30 char line[MAX_LINE];
   31 int numberOfTimes;
   32 int numberOfComputers;
   33 char command;
   34 int c1;
   35 int c2;
   36 int computers[MAX_LINE];
   37 
   38 void init()
   39 {
   40     /* FUNCTION init */
   41     scanf("%d ", &numberOfTimes);
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 void getInput()
   50 {
   51     /* FUNCTION getInput */
   52     scanf(" %d ", &numberOfComputers);
   53 } /* FUNCTION getInput */
   54 
   55 int getLogLine()
   56 {
   57     /* FUNCTION getLogLine */
   58     int dataReadFlag;
   59 
   60     fgets(line, MAX_LINE, stdin);
   61     line[strlen(line) - 1] = 0;
   62     if ((2 > strlen(line)) || (feof(stdin)))
   63         dataReadFlag = FALSE;
   64     else
   65         {
   66             /* something to read */
   67             dataReadFlag = TRUE;
   68             command = line[0];
   69             line[0] = ' ';
   70             sscanf(line, " %d %d", &c1, &c2);
   71         } /* something to read */
   72     return (dataReadFlag);
   73 } /* FUNCTION getLogLine */
   74 
   75 void process()
   76 {
   77     /* FUNCTION process */
   78     int anotherLine;
   79     int i;
   80     int currentSetCount = 1;
   81     int tmp;
   82     int connected = 0;
   83     int disconnected = 0;
   84 
   85     for (i=0; i<=numberOfComputers; i++)
   86         {
   87             computers[i] = 0;
   88         }
   89     anotherLine = getLogLine();
   90     while (anotherLine)
   91         {
   92             /* process line */
   93             DEBUG printf("%c %d %d\n", command, c1, c2);
   94             if ('q' == command)
   95                 {
   96                     /* do a query */
   97                     /* computers are connected is they are the same computer or if they have the same set number and it is not 0 */
   98                     if ((c1 == c2) || (((0 != computers[c1]) && (computers[c1] == computers[c2]))))
   99                         {
  100                             /* connected */
  101                             connected++;
  102                         } /* connected */
  103                     else
  104                         {
  105                             /* disconnected */
  106                             disconnected++;
  107                         } /* disconnected */
  108                 } /* do a query */
  109             else if ('c' == command)
  110                 {
  111                     /* add a connection */
  112                     if (0 == computers[c1])
  113                         {
  114                             /* c1 is not  connected */
  115                             if (0 == computers[c2])
  116                                 {
  117                                     /* neither computer connected */
  118                                     computers[c1] = currentSetCount;
  119                                     computers[c2] = currentSetCount;
  120                                     currentSetCount++;
  121                                 } /* neither computer connected */
  122                             else
  123                                 {
  124                                     /* only c2 is connected */
  125                                     computers[c1] = computers[c2];
  126                                 } /* only c2 is connected */
  127                         } /* c1 is not  connected */
  128                     else
  129                         {
  130                             /* c1 is connected */
  131                             if (0 == computers[c2])
  132                                 {
  133                                     /* c1 connected, but c2 is not connected */
  134                                     computers[c2] = computers[c1];
  135                                 } /* c1 connected, but c2 is not connected */
  136                             else
  137                                 {
  138                                     /* both are connected */
  139                                     tmp = computers[c2];
  140                                     for (i=0; i<=numberOfComputers; i++)
  141                                         {
  142                                             /* for - change c2's set to c1 */
  143                                             if (tmp == computers[i])
  144                                                 {
  145                                                     computers[i] = computers[c1];
  146                                                 }
  147                                         } /* for - change c2's set to c1 */
  148                                 } /* both are connected */
  149                         } /* c1 is  connected */
  150                 } /* add a connection */
  151             anotherLine = getLogLine();
  152         } /* process line */
  153     printf("%d,%d\n", connected, disconnected);
  154 } /* FUNCTION process */
  155 
  156 int main()
  157 {
  158     /* main */
  159     int i;
  160 
  161     init();
  162     for (i=0; i<numberOfTimes; i++)
  163         {
  164             /* while */
  165             if (0 != i)
  166                 {
  167                     printf("\n");
  168                 }
  169             getInput();
  170             process();
  171         } /* while */
  172 
  173     return EXIT_SUCCESS;
  174 } /* main */
  175 
  176