Problem p02827 (C++)

Buggy submission (status: Runtime Error):
// #include"stdafx.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

// int mod = 998244353;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<ll>> vvl;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ld, ld>> vpld;
typedef vector<pair<int, int>> vpi;
typedef pair<ld, ld> pld;
#define mp make_pair
#define pb push_back

int mod = 998244353;
int sum(int a, int b) {
  int c = a + b;
  if (c >= mod) {
    c -= mod;
  }
  return c;
}
int dif(int a, int b) {
  int c = a - b;
  if (c < 0) {
    c += mod;
  }
  return c;
}
int mlt(int a, int b) {
  ll c = a * 1LL * b;
  return c % mod;
}
int ibit(int n, int i) { return ((n >> i) & 1); }
void outp(vvi &ou) {
  for (int i = 0; i < ou.size(); i++) {
    for (int j = 0; j < ou[i].size(); j++) {
      cout << ou[i][j] << ' ';
    }
    cout << '\n';
  }
}
void un(int n, int m, vpi bals) {
  if (n < 3) {
    cout << "-1" << endl;
  } else {
    vi vals(n), ovals(n), ans(m);
    for (int i = 0; i < n; i++) {
      vals[i] = i;
      ovals[i] = 1;
    }
    for (int i = m - 1; i >= 0; i--) {
      int x1 = bals[i].first, y1 = bals[i].second;
      int v1 = vals[x1], v2 = vals[y1];
      if (ovals[v1] > 1) {
        //	cerr << i << " 1" << endl;
        ans[i] = -1;
        ovals[v1]--;
        ovals[v2]++;
        vals[x1] = v2;
      } else {
        //	cerr << i << " 2" << endl;
        ans[i] = 1;
        ovals[v2]--;
        ovals[v1]++;
        vals[y1] = v1;
      }
    }
    for (int i = 0; i < m; i++) {
      if (ans[i] == -1)
        cout << 'v';
      else
        cout << '^';
    }
    cout << endl;
  }
}
void non_un(int n, int m, vpi bals) {
  int bits = 50001 / 60 + 1;
  vvl dost(n, vl(bits));
  for (int i = 0; i < n; i++) {
    ll x = i % 60;
    dost[i][i / 60] += (1LL << x);
  }
  for (int i = m - 1; i >= 0; i--) {
    //		cerr << i << ' ' << dost[3][0] << endl;
    int x1 = bals[i].first, y1 = bals[i].second;
    for (int j = 0; j < bits; j++) {
      dost[x1][j] |= dost[y1][j];
      dost[y1][j] |= dost[x1][j];
    }
  }
  vl cur = dost[0];
  for (int i = 1; i < n; i++)
    for (int j = 0; j < bits; j++)
      cur[j] &= dost[i][j];

  int fr = -1;
  for (int i = 0; i < bits; i++) {
    //	cerr << cur[i] << endl;
    if (cur[i] != 0) {
      int val = cur[i], pos = -1;
      for (int j = 0; j < 61; j++) {
        if ((val >> j) & 1) {
          pos = j;
          break;
        }
      }
      assert(pos >= 0);
      fr = i * 60 + pos;
      //		cerr << i << ' ' << fr << endl;
      break;
    }
  }
  if (fr == -1) {
    cout << "-1" << endl;
  } else {
    vi used(n), ans(m);
    used[fr] = 1;
    for (int i = m - 1; i >= 0; i--) {
      int x1 = bals[i].first, y1 = bals[i].second;
      if (used[x1])
        ans[i] = -1;
      used[x1] |= used[y1];
      used[y1] |= used[x1];
    }
    for (int i = 0; i < m; i++) {
      if (ans[i] != -1)
        cout << 'v';
      else
        cout << '^';
    }
    cout << endl;
  }
}
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n, m, t;
  cin >> n >> m >> t;
  vpi bals;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    bals.pb(mp(x - 1, y - 1));
  }
  if (t == 1) {
    non_un(n, m, bals);
  } else {
    un(n, m, bals);
  }
}
int main() {
  solve();
  int n;
  cin >> n;
}

Fixed submission:
// #include"stdafx.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

// int mod = 998244353;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<ll>> vvl;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ld, ld>> vpld;
typedef vector<pair<int, int>> vpi;
typedef pair<ld, ld> pld;
#define mp make_pair
#define pb push_back

int mod = 998244353;
int sum(int a, int b) {
  int c = a + b;
  if (c >= mod) {
    c -= mod;
  }
  return c;
}
int dif(int a, int b) {
  int c = a - b;
  if (c < 0) {
    c += mod;
  }
  return c;
}
int mlt(int a, int b) {
  ll c = a * 1LL * b;
  return c % mod;
}
int ibit(int n, int i) { return ((n >> i) & 1); }
void outp(vvi &ou) {
  for (int i = 0; i < ou.size(); i++) {
    for (int j = 0; j < ou[i].size(); j++) {
      cout << ou[i][j] << ' ';
    }
    cout << '\n';
  }
}
void un(int n, int m, vpi bals) {
  if (n < 3) {
    cout << "-1" << endl;
  } else {
    vi vals(n), ovals(n), ans(m);
    for (int i = 0; i < n; i++) {
      vals[i] = i;
      ovals[i] = 1;
    }
    for (int i = m - 1; i >= 0; i--) {
      int x1 = bals[i].first, y1 = bals[i].second;
      int v1 = vals[x1], v2 = vals[y1];
      if (ovals[v1] > 1) {
        //	cerr << i << " 1" << endl;
        ans[i] = -1;
        ovals[v1]--;
        ovals[v2]++;
        vals[x1] = v2;
      } else {
        //	cerr << i << " 2" << endl;
        ans[i] = 1;
        ovals[v2]--;
        ovals[v1]++;
        vals[y1] = v1;
      }
    }
    for (int i = 0; i < m; i++) {
      if (ans[i] == -1)
        cout << 'v';
      else
        cout << '^';
    }
    cout << endl;
  }
}
void non_un(int n, int m, vpi bals) {
  int bits = 50001 / 60 + 1;
  vvl dost(n, vl(bits));
  for (int i = 0; i < n; i++) {
    ll x = i % 60;
    dost[i][i / 60] += (1LL << x);
  }
  for (int i = m - 1; i >= 0; i--) {
    //		cerr << i << ' ' << dost[3][0] << endl;
    int x1 = bals[i].first, y1 = bals[i].second;
    for (int j = 0; j < bits; j++) {
      dost[x1][j] |= dost[y1][j];
      dost[y1][j] |= dost[x1][j];
    }
  }
  vl cur = dost[0];
  for (int i = 1; i < n; i++)
    for (int j = 0; j < bits; j++)
      cur[j] &= dost[i][j];

  int fr = -1;
  for (int i = 0; i < bits; i++) {
    //	cerr << cur[i] << endl;
    if (cur[i] != 0) {
      ll val = cur[i], pos = -1;
      for (int j = 0; j < 61; j++) {
        if ((val >> j) & 1) {
          pos = j;
          break;
        }
      }
      assert(pos >= 0);
      fr = i * 60 + pos;
      //		cerr << i << ' ' << fr << endl;
      break;
    }
  }
  if (fr == -1) {
    cout << "-1" << endl;
  } else {
    vi used(n), ans(m);
    used[fr] = 1;
    for (int i = m - 1; i >= 0; i--) {
      int x1 = bals[i].first, y1 = bals[i].second;
      if (used[x1])
        ans[i] = -1;
      used[x1] |= used[y1];
      used[y1] |= used[x1];
    }
    for (int i = 0; i < m; i++) {
      if (ans[i] != -1)
        cout << 'v';
      else
        cout << '^';
    }
    cout << endl;
  }
}
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n, m, t;
  cin >> n >> m >> t;
  vpi bals;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    bals.pb(mp(x - 1, y - 1));
  }
  if (t == 1) {
    non_un(n, m, bals);
  } else {
    un(n, m, bals);
  }
}
int main() {
  solve();
  int n;
  cin >> n;
}