Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#ifndef ULLONG
typedef unsigned long long ULLONG;
#endif
#ifndef LLONG
typedef long long LLONG;
#endif
template <class _Ty> static _Ty tp_abs(const _Ty &a) {
  return ((a < 0) ? (a * -1) : a);
}
template <class _Ty> static _Ty tp_min(_Ty a, _Ty b) { return a < b ? a : b; }
template <class _Ty> static _Ty tp_max(_Ty a, _Ty b) { return a > b ? a : b; }
template <class T> static void get1int(T &a) {
  const char *fmt = " %d";
  if (sizeof(T) == sizeof(long long)) {
    fmt = " %lld";
  }

  if (scanf(fmt, &a) < 0) {
    printf("g1int Error\n");
  }
}
template <class T> static void get2int(T &a, T &b) {
  const char *fmt = " %d %d";
  if (sizeof(T) == sizeof(long long)) {
    fmt = " %lld %lld";
  }

  if (scanf(fmt, &a, &b) < 0) {
    printf("g2int Error\n");
  }
}
template <class T> static void get3int(T &a, T &b, T &c) {
  const char *fmt = " %d %d %d";
  if (sizeof(T) == sizeof(long long)) {
    fmt = " %lld %lld %lld";
  }

  if (scanf(fmt, &a, &b, &c) < 0) {
    printf("g3int Error\n");
  }
}

static const int EduDp_INF = 1100000000;

static void G_task();

#if 1
int main() {
  G_task();
  fflush(stdout);
  return 0;
}
#endif

static int dfs(std::vector<std::vector<int>> graph, int vert, int vCount,
               int *dp) {
  if (vert >= vCount) {
    return 0;
  }
  if (dp[vert] >= 0) {
    return dp[vert];
  }

  int len = 0;
  int from = vert;
  int pass = graph[from].size();
  if (pass > 0) {
    for (int i = 0; i < pass; i++) {
      len = tp_max(dfs(graph, graph[from][i], vCount, dp), len);
    }
    len++;
  }
  dp[vert] = len;
  return len;
}

static void G_task() {
  const int GMAX = 100000;

  int N, M;
  get2int(N, M);

  std::vector<std::vector<int>> edge(N);
  for (int e = 0; e < M; e++) {
    int from, to;
    get2int(from, to);
    edge[from - 1].push_back(to - 1);
  }

  auto dp = new int[N + 1];
  memset(dp, -1, sizeof(dp[0]) * (N + 1));
  int max = 0;
  for (int n = 0; n < N; n++) {
    max = tp_max(max, dfs(edge, n, N, dp));
  }

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

  delete[] dp;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#ifndef ULLONG
typedef unsigned long long ULLONG;
#endif
#ifndef LLONG
typedef long long LLONG;
#endif
template <class _Ty> static _Ty tp_abs(const _Ty &a) {
  return ((a < 0) ? (a * -1) : a);
}
template <class _Ty> static _Ty tp_min(_Ty a, _Ty b) { return a < b ? a : b; }
template <class _Ty> static _Ty tp_max(_Ty a, _Ty b) { return a > b ? a : b; }
template <class T> static void get1int(T &a) {
  const char *fmt = " %d";
  if (sizeof(T) == sizeof(long long)) {
    fmt = " %lld";
  }

  if (scanf(fmt, &a) < 0) {
    printf("g1int Error\n");
  }
}
template <class T> static void get2int(T &a, T &b) {
  const char *fmt = " %d %d";
  if (sizeof(T) == sizeof(long long)) {
    fmt = " %lld %lld";
  }

  if (scanf(fmt, &a, &b) < 0) {
    printf("g2int Error\n");
  }
}
template <class T> static void get3int(T &a, T &b, T &c) {
  const char *fmt = " %d %d %d";
  if (sizeof(T) == sizeof(long long)) {
    fmt = " %lld %lld %lld";
  }

  if (scanf(fmt, &a, &b, &c) < 0) {
    printf("g3int Error\n");
  }
}

static const int EduDp_INF = 1100000000;

static void G_task();

#if 1
int main() {
  G_task();
  fflush(stdout);
  return 0;
}
#endif

static int dfs(std::vector<std::vector<int>> &graph, int vert, int vCount,
               int *dp) {
  if (vert >= vCount) {
    return 0;
  }
  if (dp[vert] >= 0) {
    return dp[vert];
  }

  int len = 0;
  int from = vert;
  int pass = graph[from].size();
  if (pass > 0) {
    for (int i = 0; i < pass; i++) {
      len = tp_max(dfs(graph, graph[from][i], vCount, dp), len);
    }
    len++;
  }
  dp[vert] = len;
  return len;
}

static void G_task() {
  const int GMAX = 100000;

  int N, M;
  get2int(N, M);

  std::vector<std::vector<int>> edge(N);
  for (int e = 0; e < M; e++) {
    int from, to;
    get2int(from, to);
    edge[from - 1].push_back(to - 1);
  }

  auto dp = new int[N + 1];
  memset(dp, -1, sizeof(dp[0]) * (N + 1));
  int max = 0;
  for (int n = 0; n < N; n++) {
    max = tp_max(max, dfs(edge, n, N, dp));
  }

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

  delete[] dp;
}