Problem p03650 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, vis[N], p[N];
int f[N], g[N], dp[N], tong[N];
vector<int> v[N];
void dfs(int x) {
  for (int i = 0; i < v[x].size(); i++)
    dfs(v[x][i]);
  for (int i = 0; i < v[x].size(); i++)
    tong[dp[v[x][i]]]++;
  for (; tong[dp[x]]; dp[x]++)
    ;
  for (int i = 0; i < v[x].size(); i++)
    tong[dp[v[x][i]]]--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    v[p[i]].push_back(i);
  }
  for (int i = 1;; i = p[i]) {
    vis[i]++;
    if (vis[i] > 2)
      break;
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] >= 2) {
      for (int j = 0; j < v[i].size(); j++)
        if (vis[v[i][j]] < 2)
          dfs(v[i][j]);
      for (int j = 0; j < v[i].size(); j++)
        if (vis[v[i][j]] < 2)
          tong[dp[v[i][j]]]++;
      f[i] = g[i] = -1;
      for (int j = 0;; j++)
        if (!tong[j]) {
          if (f[i] == -1)
            f[i] = j;
          else if (g[i] == -1)
            g[i] = j;
          else
            break;
        }
      for (int j = 0; j < v[i].size(); j++)
        if (vis[v[i][j]] < 2)
          tong[dp[v[i][j]]]--;
    }
  for (int i = 1; i <= n; i++)
    if (vis[i] >= 2) {
      dp[i] = f[i];
      for (int j = i;; j = p[j]) {
        if (p[j] == i) {
          if (dp[j] ^ dp[i])
            return puts("POSSIBLE"), 0;
          break;
        }
        dp[p[j]] = (f[p[j]] == dp[j] ? g[p[j]] : f[p[j]]);
      }
      dp[i] = g[i];
      for (int j = i;; j = p[j]) {
        if (p[j] == i) {
          if (dp[j] == f[i])
            return puts("POSSIBLE"), 0;
          break;
        }
        dp[p[j]] = (f[p[j]] == dp[j] ? g[p[j]] : f[p[j]]);
      }
      return puts("IMPOSSIBLE");
    }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, vis[N], p[N];
int f[N], g[N], dp[N], tong[N];
vector<int> v[N];
void dfs(int x) {
  for (int i = 0; i < v[x].size(); i++)
    dfs(v[x][i]);
  for (int i = 0; i < v[x].size(); i++)
    tong[dp[v[x][i]]]++;
  for (; tong[dp[x]]; dp[x]++)
    ;
  for (int i = 0; i < v[x].size(); i++)
    tong[dp[v[x][i]]]--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    v[p[i]].push_back(i);
  }
  for (int i = 1;; i = p[i]) {
    vis[i]++;
    if (vis[i] > 2)
      break;
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] >= 2) {
      for (int j = 0; j < v[i].size(); j++)
        if (vis[v[i][j]] < 2)
          dfs(v[i][j]);
      for (int j = 0; j < v[i].size(); j++)
        if (vis[v[i][j]] < 2)
          tong[dp[v[i][j]]]++;
      f[i] = g[i] = -1;
      for (int j = 0;; j++)
        if (!tong[j]) {
          if (f[i] == -1)
            f[i] = j;
          else if (g[i] == -1)
            g[i] = j;
          else
            break;
        }
      for (int j = 0; j < v[i].size(); j++)
        if (vis[v[i][j]] < 2)
          tong[dp[v[i][j]]]--;
    }
  for (int i = 1; i <= n; i++)
    if (vis[i] >= 2) {
      dp[i] = f[i];
      for (int j = i;; j = p[j]) {
        if (p[j] == i) {
          if (dp[j] ^ dp[i])
            return puts("POSSIBLE"), 0;
          break;
        }
        dp[p[j]] = (f[p[j]] == dp[j] ? g[p[j]] : f[p[j]]);
      }
      dp[i] = g[i];
      for (int j = i;; j = p[j]) {
        if (p[j] == i) {
          if (dp[j] == f[i])
            return puts("POSSIBLE"), 0;
          break;
        }
        dp[p[j]] = (f[p[j]] == dp[j] ? g[p[j]] : f[p[j]]);
      }
      return puts("IMPOSSIBLE"), 0;
    }
}