Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define enter putchar('\n')
#define Mem(a, x) memset(a, x, sizeof(a))
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

inline ll read() {
  ll x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x = f ? -x : x;
}

inline void write(ll x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char s[23];
  int l = 0;
  while (x != 0)
    s[l++] = x % 10 + 48, x /= 10;
  while (l)
    putchar(s[--l]);
}

template <class T, class U> inline void checkMax(T &x, U y) {
  if (y > x)
    x = y;
}

const int M = 1123456; // 2e5+5

int n;
vector<int> to[M];
int fa[M];
int inDg[M];
set<int> points;
int cnt = 0;

void dfs(int now) {
  cnt++;
  for (auto son : to[now]) {
    inDg[son]--;
    if (inDg[son] == 0) {
      dfs(son);
    }
  }
}

int dp[M];

int dfs2(int now) {
  if (dp[now] != -1)
    return dp[now];
  int tmp = 0;
  for (auto son : to[now]) {
    checkMax(tmp, dfs2(son) + 1);
  }
  return dp[now] = tmp;
}

void init() {
  Mem(dp, -1);
  n = read();
  for (int i = 1; i <= n; ++i) {
    int bf = 0;
    points.insert(0);
    for (int j = 1; j < n; ++j) {
      int tmp1 = read();
      int tmp2 = i;
      if (tmp1 > tmp2)
        swap(tmp1, tmp2);
      int tmp = tmp1 * 999 + tmp2;
      points.insert(tmp);
      fa[tmp] = bf;
      to[bf].emplace_back(tmp);
      inDg[tmp]++;
      bf = tmp;
    }
  }
  dfs(0);
  if (cnt < points.size()) {
    puts("-1");
    return;
  }
  write(dfs2(0)), enter;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  init();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define enter putchar('\n')
#define Mem(a, x) memset(a, x, sizeof(a))
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

inline ll read() {
  ll x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x = f ? -x : x;
}

inline void write(ll x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char s[23];
  int l = 0;
  while (x != 0)
    s[l++] = x % 10 + 48, x /= 10;
  while (l)
    putchar(s[--l]);
}

template <class T, class U> inline void checkMax(T &x, U y) {
  if (y > x)
    x = y;
}

const int M = 1123456; // 2e5+5

int n;
vector<int> to[M];
int fa[M];
int inDg[M];
set<int> points;
int cnt = 0;

void dfs(int now) {
  cnt++;
  for (auto son : to[now]) {
    inDg[son]--;
    if (inDg[son] == 0) {
      dfs(son);
    }
  }
}

int dp[M];

int dfs2(int now) {
  if (dp[now] != -1)
    return dp[now];
  int tmp = 0;
  for (auto son : to[now]) {
    checkMax(tmp, dfs2(son) + 1);
  }
  return dp[now] = tmp;
}

void init() {
  Mem(dp, -1);
  n = read();
  for (int i = 1; i <= n; ++i) {
    int bf = 0;
    points.insert(0);
    for (int j = 1; j < n; ++j) {
      int tmp1 = read();
      int tmp2 = i;
      if (tmp1 > tmp2)
        swap(tmp1, tmp2);
      int tmp = tmp1 * 999 + tmp2;
      points.insert(tmp);
      fa[tmp] = bf;
      to[bf].emplace_back(tmp);
      inDg[tmp]++;
      bf = tmp;
    }
  }
  dfs(0);
  if (cnt < points.size()) {
    puts("-1");
    return;
  }
  write(dfs2(0)), enter;
}

int main() {

  init();
  return 0;
}