Problem p00715 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  char name[20];
};

int N, table[200][200], index;
Info info[200];

bool strCmp(char *base, char *comp) {
  int length1, length2;
  for (length1 = 0; base[length1] != '\0'; length1++)
    ;
  for (length2 = 0; comp[length2] != '\0'; length2++)
    ;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\0';
  }
}

bool check(int A, int B) {

  bool FLG = false;

  for (int k = 0; k < index; k++) {
    if (k == A || k == B)
      continue;

    if ((table[k][A] == 1 && table[k][B] == 1) ||
        (table[A][k] == 1 && table[B][k] == 1)) {
      FLG = true;
    } else if ((table[k][A] == 1 && table[B][k] == 1) ||
               (table[A][k] == 1 && table[k][B] == 1)) {
      return false;
    }
  }

  return FLG;
}

void func() {

  for (int i = 0; i < 200; i++) {
    for (int k = 0; k < 200; k++) {
      table[i][k] = BIG_NUM;
    }
  }

  index = 0;
  int work_index, left_id, right_id, start;
  char buf[20], work[20];

  bool FLG;

  for (int i = 0; i < N; i++) {
    scanf("%s", buf);
    for (work_index = 0; buf[work_index] != '-'; work_index++) {
      work[work_index] = buf[work_index];
    }
    work[work_index] = '\0';

    FLG = false;
    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        left_id = k;
        FLG = true;
        break;
      }
    }
    if (!FLG) {
      left_id = index;
      strcpy(info[index].name, work);
      index++;
    }

    start = work_index + 1;

    for (work_index = 0; buf[start + work_index] != '\0'; work_index++) {
      work[work_index] = buf[start + work_index];
    }
    work[work_index] = '\0';

    FLG = false;

    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        right_id = k;
        FLG = true;
        break;
      }
    }
    if (!FLG) {
      right_id = index;
      strcpy(info[index].name, work);
      index++;
    }
    table[left_id][right_id] = 1;
  }

  for (int i = 0; i < index - 1; i++) {
    for (int k = i + 1; k < index; k++) {
      if (check(i, k)) {
        table[i][k] = 2;
        table[k][i] = 2;
      }
    }
  }

  for (int mid = 0; mid < index; mid++) {
    for (int start = 0; start < index; start++) {
      if (table[start][mid] == BIG_NUM)
        continue;
      for (int goal = 0; goal < index; goal++) {
        if (table[mid][goal] == BIG_NUM)
          continue;
        table[start][goal] =
            min(table[start][goal], table[start][mid] + table[mid][goal]);
      }
    }
  }

  printf("%d\n", index);

  int M;
  scanf("%d", &M);

  for (int loop = 0; loop < M; loop++) {
    scanf("%s", buf);
    for (work_index = 0; buf[work_index] != '-'; work_index++) {
      work[work_index] = buf[work_index];
    }
    work[work_index] = '\0';

    FLG = false;
    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        left_id = k;
        FLG = true;
        break;
      }
    }
    if (!FLG) {
      printf("NO\n");
      continue;
    }

    start = work_index + 1;

    for (work_index = 0; buf[start + work_index] != '\0'; work_index++) {
      work[work_index] = buf[start + work_index];
    }
    work[work_index] = '\0';

    FLG = false;

    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        right_id = k;
        FLG = true;
        break;
      }
    }

    if (!FLG) {
      printf("NO\n");
      continue;
    }

    if (table[left_id][right_id] % 2 == 1) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func();
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  char name[20];
};

int N, table[200][200], index;
Info info[200];

bool strCmp(char *base, char *comp) {
  int length1, length2;
  for (length1 = 0; base[length1] != '\0'; length1++)
    ;
  for (length2 = 0; comp[length2] != '\0'; length2++)
    ;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\0';
  }
}

bool check(int A, int B) {

  bool FLG = false;

  for (int k = 0; k < index; k++) {
    if (k == A || k == B)
      continue;

    if ((table[k][A] == 1 && table[k][B] == 1) ||
        (table[A][k] == 1 && table[B][k] == 1)) {
      FLG = true;
    } else if ((table[k][A] == 1 && table[B][k] == 1) ||
               (table[A][k] == 1 && table[k][B] == 1)) {
      return false;
    }
  }

  return FLG;
}

void func() {

  for (int i = 0; i < 200; i++) {
    for (int k = 0; k < 200; k++) {
      table[i][k] = BIG_NUM;
    }
  }

  index = 0;
  int work_index, left_id, right_id, start;
  char buf[40], work[20];

  bool FLG;

  for (int i = 0; i < N; i++) {
    scanf("%s", buf);
    for (work_index = 0; buf[work_index] != '-'; work_index++) {
      work[work_index] = buf[work_index];
    }
    work[work_index] = '\0';

    FLG = false;
    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        left_id = k;
        FLG = true;
        break;
      }
    }
    if (!FLG) {
      left_id = index;
      strcpy(info[index].name, work);
      index++;
    }

    start = work_index + 1;

    for (work_index = 0; buf[start + work_index] != '\0'; work_index++) {
      work[work_index] = buf[start + work_index];
    }
    work[work_index] = '\0';

    FLG = false;

    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        right_id = k;
        FLG = true;
        break;
      }
    }
    if (!FLG) {
      right_id = index;
      strcpy(info[index].name, work);
      index++;
    }
    table[left_id][right_id] = 1;
  }

  for (int i = 0; i < index - 1; i++) {
    for (int k = i + 1; k < index; k++) {
      if (check(i, k)) {
        table[i][k] = 2;
        table[k][i] = 2;
      }
    }
  }

  for (int mid = 0; mid < index; mid++) {
    for (int start = 0; start < index; start++) {
      if (table[start][mid] == BIG_NUM)
        continue;
      for (int goal = 0; goal < index; goal++) {
        if (table[mid][goal] == BIG_NUM)
          continue;
        table[start][goal] =
            min(table[start][goal], table[start][mid] + table[mid][goal]);
      }
    }
  }

  printf("%d\n", index);

  int M;
  scanf("%d", &M);

  for (int loop = 0; loop < M; loop++) {
    scanf("%s", buf);
    for (work_index = 0; buf[work_index] != '-'; work_index++) {
      work[work_index] = buf[work_index];
    }
    work[work_index] = '\0';

    FLG = false;
    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        left_id = k;
        FLG = true;
        break;
      }
    }
    if (!FLG) {
      printf("NO\n");
      continue;
    }

    start = work_index + 1;

    for (work_index = 0; buf[start + work_index] != '\0'; work_index++) {
      work[work_index] = buf[start + work_index];
    }
    work[work_index] = '\0';

    FLG = false;

    for (int k = 0; k < index; k++) {
      if (strCmp(info[k].name, work)) {
        right_id = k;
        FLG = true;
        break;
      }
    }

    if (!FLG) {
      printf("NO\n");
      continue;
    }

    if (table[left_id][right_id] % 2 == 1) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func();
  }

  return 0;
}