Problem p02827 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

int n, m, t;
int x[100005], y[100005];
int ans[100005];
int val[100005];
map<int, int> M;
bitset<50005> B[50005];

void solve_hard() {
  repn(i, n) B[i][i] = 1;
  rep(j, m) {
    B[x[j]] |= B[y[j]];
    B[y[j]] |= B[x[j]];
  }
  for (int i = 2; i <= n; i++)
    B[1] &= B[i];
  int v = -1;
  rep(i, 50005) {
    if (B[1][i] == 1) {
      v = i;
      break;
    }
  }
  if (v == -1) {
    puts("-1");
    return;
  }
  string ans = "";
  repn(i, n) { val[i] = i; }
  for (int j = m - 1; j >= 0; j--) {
    int f1 = val[x[j]] == v;
    int f2 = val[y[j]] == v;

    if (f1 && f2) {
      val[y[j]] = val[x[j]];
      ans.pb('^');
    } else if (!f1 && !f2) {
      val[y[j]] = val[x[j]];
      ans.pb('^');
    } else if (f1 && !f2) {
      val[y[j]] = val[x[j]];
      ans.pb('^');
    } else {
      val[x[j]] = val[y[j]];
      ans.pb('v');
    }
  }
  repn(i, n) assert(val[i] == v);
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
int main() {
  scanf("%d%d%d", &n, &m, &t);
  rep(i, m) scanf("%d%d", &x[i], &y[i]);

  if (t == 1) {
    solve_hard();
    return 0;
  }
  repn(i, n) {
    val[i] = i;
    M[i]++;
  }
  for (int i = m - 1; i >= 0; i--) {
    int a = val[x[i]];
    int b = val[y[i]];
    if (M[a] < M[b]) {
      ans[i] = 1;
      M[a]++;
      M[b]--;
      val[y[i]] = a;
    } else {
      ans[i] = 0;
      M[a]--;
      M[b]++;
      val[x[i]] = b;
    }
  }
  for (auto a : M) {
    if (a.sc == n) {
      puts("-1");
      return 0;
    }
  }
  rep(i, m) printf(!ans[i] ? "v" : "^");
  puts("");
  // repn(i,n) cout << val[i] << endl;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

int n, m, t;
int x[100005], y[100005];
int ans[100005];
int val[100005];
map<int, int> M;
bitset<50005> B[50005];

void solve_hard() {
  repn(i, n) B[i][i] = 1;
  for (int j = m - 1; j >= 0; j--) {
    B[x[j]] |= B[y[j]];
    B[y[j]] |= B[x[j]];
  }
  for (int i = 2; i <= n; i++)
    B[1] &= B[i];
  int v = -1;
  rep(i, 50005) {
    if (B[1][i] == 1) {
      v = i;
      break;
    }
  }
  if (v == -1) {
    puts("-1");
    return;
  }
  string ans = "";
  repn(i, n) { val[i] = i; }
  for (int j = m - 1; j >= 0; j--) {
    int f1 = val[x[j]] == v;
    int f2 = val[y[j]] == v;

    if (f1 && f2) {
      val[y[j]] = val[x[j]];
      ans.pb('^');
    } else if (!f1 && !f2) {
      val[y[j]] = val[x[j]];
      ans.pb('^');
    } else if (f1 && !f2) {
      val[y[j]] = val[x[j]];
      ans.pb('^');
    } else {
      val[x[j]] = val[y[j]];
      ans.pb('v');
    }
  }
  repn(i, n) assert(val[i] == v);
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
}
int main() {
  scanf("%d%d%d", &n, &m, &t);
  rep(i, m) scanf("%d%d", &x[i], &y[i]);

  if (t == 1) {
    solve_hard();
    return 0;
  }
  repn(i, n) {
    val[i] = i;
    M[i]++;
  }
  for (int i = m - 1; i >= 0; i--) {
    int a = val[x[i]];
    int b = val[y[i]];
    if (M[a] < M[b]) {
      ans[i] = 1;
      M[a]++;
      M[b]--;
      val[y[i]] = a;
    } else {
      ans[i] = 0;
      M[a]--;
      M[b]++;
      val[x[i]] = b;
    }
  }
  for (auto a : M) {
    if (a.sc == n) {
      puts("-1");
      return 0;
    }
  }
  rep(i, m) printf(!ans[i] ? "v" : "^");
  puts("");
  // repn(i,n) cout << val[i] << endl;
}