Problem p02827 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

#define pii pair<int, int>

const int N = 5e4 + 5;
const int M = 1e5 + 5;

int n, m, type;
char ans[M];
int us[M], vs[M];

namespace subtask1 {

bitset<N> f[M];
boolean vis[M];
int h[N], A[M], B[M];

void F(int p, int x) {
  if (!p || vis[p]) {
    return;
  }
  vis[p] = true;
  if (x == us[p]) {
    F(A[p], x);
    F(B[p], vs[p]);
  } else {
    ans[p] = 'v';
    F(B[p], x);
    F(A[p], us[p]);
  }
}

void solve() {
  for (int i = 1, u, v; i <= m; i++) {
    u = us[i], v = vs[i];
    if (!h[u]) {
      f[i].set(u);
    } else {
      f[i] = f[h[u]];
    }
    swap(u, v);
    if (!h[u]) {
      f[i].set(u);
    } else {
      f[i] |= f[h[u]];
    }
    swap(u, v);
    A[i] = h[u], B[i] = h[v];
    h[u] = h[v] = i;
  }
  for (int i = 1; i <= m; i++)
    ans[i] = '^';
  for (int i = 1; i <= m; i++) {
    if ((signed)f[i].count() == n) {
      F(i, us[i]);
      puts(ans + 1);
      return;
    }
  }
  puts("-1");
}

} // namespace subtask1

namespace subtask2 {

const int C = 320;

set<pii> S;
int h[C];
int nxt[M][C];
bitset<C> vis[M][2];

boolean dfs(int p, int x, int y) {
  if (x == y) {
    return false;
  }
  if (p == m + 1) {
    return true;
  }
  int sx = -1, sy = -1;
  if (x == us[p] || y == us[p]) {
    sx = 0, sy = x ^ y ^ us[p];
  } else {
    sx = 1, sy = x ^ y ^ vs[p];
  }
  if (vis[p][sx].test(sy))
    return false;
  vis[p][sx].set(sy);

  // down
  int nx = (x == us[p]) ? vs[p] : x;
  int ny = (y == us[p]) ? vs[p] : y;
  if (dfs(min(nxt[p][nx], nxt[p][ny]), nx, ny)) {
    ans[p] = 'v';
    return true;
  }

  nx = (x == vs[p]) ? us[p] : x;
  ny = (y == vs[p]) ? us[p] : y;
  if (dfs(min(nxt[p][nx], nxt[p][ny]), nx, ny)) {
    ans[p] = '^';
    return true;
  }
  return false;
}

void solve() {
  for (int i = 1; i <= m; i++) {
    S.insert(pii(us[i], vs[i]));
  }
  if ((signed)S.size() < 1ll * n * (n - 1) / 2) {
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (!S.count(pii(i, j))) {
          for (int k = 1; k <= m; k++) {
            if (us[k] == i || us[k] == j) {
              ans[k] = '^';
            } else {
              ans[k] = 'v';
            }
          }
          puts(ans + 1);
          return;
        }
      }
    }
    assert(false);
    return;
  }
  assert(n < C);
  for (int i = 1; i <= m; i++)
    ans[i] = 'v';
  for (int i = 1; i <= n; i++)
    h[i] = m + 1;
  for (int i = m; i; i--) {
    memcpy(nxt[i], h, sizeof(h));
    h[us[i]] = h[vs[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (dfs(min(h[i], h[j]), i, j)) {
        puts(ans + 1);
        return;
      }
    }
  }
  puts("-1");
}

} // namespace subtask2

int main() {
  scanf("%d%d%d", &n, &m, &type);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", us + i, vs + i);
    ;
  }
  if (type == 1) {
    subtask1::solve();
  } else {
    subtask2::solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

#define pii pair<int, int>

const int N = 5e4 + 5;
const int M = 1e5 + 5;

int n, m, type;
char ans[M];
int us[M], vs[M];

namespace subtask1 {

bitset<N> f[M];
boolean vis[M];
int h[N], A[M], B[M];

void F(int p, int x) {
  if (!p || vis[p]) {
    return;
  }
  vis[p] = true;
  if (x == us[p]) {
    F(A[p], x);
    F(B[p], vs[p]);
  } else {
    ans[p] = 'v';
    F(B[p], x);
    F(A[p], us[p]);
  }
}

void solve() {
  for (int i = 1, u, v; i <= m; i++) {
    u = us[i], v = vs[i];
    if (!h[u]) {
      f[i].set(u);
    } else {
      f[i] = f[h[u]];
    }
    swap(u, v);
    if (!h[u]) {
      f[i].set(u);
    } else {
      f[i] |= f[h[u]];
    }
    swap(u, v);
    A[i] = h[u], B[i] = h[v];
    h[u] = h[v] = i;
  }
  for (int i = 1; i <= m; i++)
    ans[i] = '^';
  for (int i = 1; i <= m; i++) {
    if ((signed)f[i].count() == n) {
      F(i, us[i]);
      puts(ans + 1);
      return;
    }
  }
  puts("-1");
}

} // namespace subtask1

namespace subtask2 {

const int C = 451;

set<pii> S;
int h[C];
int nxt[M][C];
bitset<C> vis[M][2];

boolean dfs(int p, int x, int y) {
  if (x == y) {
    return false;
  }
  if (p == m + 1) {
    return true;
  }
  int sx = -1, sy = -1;
  if (x == us[p] || y == us[p]) {
    sx = 0, sy = x ^ y ^ us[p];
  } else {
    sx = 1, sy = x ^ y ^ vs[p];
  }
  if (vis[p][sx].test(sy))
    return false;
  vis[p][sx].set(sy);

  // down
  int nx = (x == us[p]) ? vs[p] : x;
  int ny = (y == us[p]) ? vs[p] : y;
  if (dfs(min(nxt[p][nx], nxt[p][ny]), nx, ny)) {
    ans[p] = 'v';
    return true;
  }

  nx = (x == vs[p]) ? us[p] : x;
  ny = (y == vs[p]) ? us[p] : y;
  if (dfs(min(nxt[p][nx], nxt[p][ny]), nx, ny)) {
    ans[p] = '^';
    return true;
  }
  return false;
}

void solve() {
  for (int i = 1; i <= m; i++) {
    S.insert(pii(us[i], vs[i]));
  }
  if ((signed)S.size() < 1ll * n * (n - 1) / 2) {
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (!S.count(pii(i, j))) {
          for (int k = 1; k <= m; k++) {
            if (us[k] == i || us[k] == j) {
              ans[k] = '^';
            } else {
              ans[k] = 'v';
            }
          }
          puts(ans + 1);
          return;
        }
      }
    }
    assert(false);
    return;
  }
  assert(n < C);
  for (int i = 1; i <= m; i++)
    ans[i] = 'v';
  for (int i = 1; i <= n; i++)
    h[i] = m + 1;
  for (int i = m; i; i--) {
    memcpy(nxt[i], h, sizeof(h));
    h[us[i]] = h[vs[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (dfs(min(h[i], h[j]), i, j)) {
        puts(ans + 1);
        return;
      }
    }
  }
  puts("-1");
}

} // namespace subtask2

int main() {
  scanf("%d%d%d", &n, &m, &type);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", us + i, vs + i);
    ;
  }
  if (type == 1) {
    subtask1::solve();
  } else {
    subtask2::solve();
  }
  return 0;
}