Problem p02827 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 50005;
const int M = 100005;

int n, m, t, i, x[M], y[M], mapa[N], co[N];
bitset<N> b[N], bb;
string s;
bool cc[N];

void solve() {
  if (n == 2) {
    printf("-1\n");
    return;
  }
  for (i = 0; i < n; i++) {
    mapa[i] = i;
    co[i] = 1;
  }
  for (i = m - 1; i >= 0; i--) {
    if (co[mapa[x[i]]] > 1) {
      s += 'v';
      co[mapa[x[i]]]--;
      mapa[x[i]] = mapa[y[i]];
      co[mapa[x[i]]]++;
    } else {
      s += '^';
      co[mapa[y[i]]]--;
      mapa[y[i]] = mapa[x[i]];
      co[mapa[y[i]]]++;
    }
  }
  int ind = -1;
  for (i = 1; i < n; i++)
    if (mapa[i] != mapa[i - 1])
      ind = i;
  int a1 = ind - 1;
  int a2 = ind;
  for (i = 0; i < m; i++) {
    if (x[i] == a1 && s[i] == 'v')
      a1 = y[i];
    else if (y[i] == a1 && s[i] == '^')
      a1 = x[i];
    if (x[i] == a2 && s[i] == 'v')
      a2 = y[i];
    else if (y[i] == a2 && s[i] == '^')
      a2 = x[i];
  }
  if (a1 == a2) {
    int a = 3;
    while (a < 10) {
      a++;
      a--;
    }
  }
  printf("%s\n", s.c_str());
}

int main() {
  scanf("%d %d %d", &n, &m, &t);
  s = "";
  for (i = 0; i < m; i++) {
    scanf("%d %d", &x[i], &y[i]);
    x[i]--;
    y[i]--;
  }
  if (t == 2) {
    solve();
    return 0;
  }
  for (i = 0; i < n; i++) {
    b[i].set(i);
    bb.set(i);
  }
  for (i = m - 1; i >= 0; i--)
    b[x[i]] = b[y[i]] = b[x[i]] | b[y[i]];
  for (i = 0; i < n; i++)
    bb &= b[i];
  for (i = 0; i < n; i++)
    if (bb[i]) {
      cc[i] = 1;
      for (i = m - 1; i >= 0; i--) {
        if (cc[y[i]])
          s += 'v';
        else
          s += '^';
        cc[x[i]] = cc[y[i]] = cc[x[i]] | cc[y[i]];
      }
      reverse(s.begin(), s.end());
      printf("%s\n", s.c_str());
      return 0;
    }
  printf("-1\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 50005;
const int M = 100005;

int n, m, t, i, x[M], y[M], mapa[N], co[N];
bitset<N> b[N], bb;
string s;
bool cc[N];

void solve() {
  if (n == 2) {
    printf("-1\n");
    return;
  }
  for (i = 0; i < n; i++) {
    mapa[i] = i;
    co[i] = 1;
  }
  for (i = m - 1; i >= 0; i--) {
    if (co[mapa[x[i]]] > 1) {
      s += 'v';
      co[mapa[x[i]]]--;
      mapa[x[i]] = mapa[y[i]];
      co[mapa[x[i]]]++;
    } else {
      s += '^';
      co[mapa[y[i]]]--;
      mapa[y[i]] = mapa[x[i]];
      co[mapa[y[i]]]++;
    }
  }
  reverse(s.begin(), s.end());
  printf("%s\n", s.c_str());
}

int main() {
  scanf("%d %d %d", &n, &m, &t);
  s = "";
  for (i = 0; i < m; i++) {
    scanf("%d %d", &x[i], &y[i]);
    x[i]--;
    y[i]--;
  }
  if (t == 2) {
    solve();
    return 0;
  }
  for (i = 0; i < n; i++) {
    b[i].set(i);
    bb.set(i);
  }
  for (i = m - 1; i >= 0; i--)
    b[x[i]] = b[y[i]] = b[x[i]] | b[y[i]];
  for (i = 0; i < n; i++)
    bb &= b[i];
  for (i = 0; i < n; i++)
    if (bb[i]) {
      cc[i] = 1;
      for (i = m - 1; i >= 0; i--) {
        if (cc[y[i]])
          s += 'v';
        else
          s += '^';
        cc[x[i]] = cc[y[i]] = cc[x[i]] | cc[y[i]];
      }
      reverse(s.begin(), s.end());
      printf("%s\n", s.c_str());
      return 0;
    }
  printf("-1\n");
  return 0;
}