Problem p02827 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#ifndef print
#define print(...)
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double f80;
#define double long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(x) ((long long)x.size())
#define fr(a, b, c) for (int a = b; a <= c; a++)
#define rep(a, b, c) for (int a = b; a < c; a++)
#define trav(a, x) for (auto &a : x)
#define all(con) con.begin(), con.end()
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
// const int mod=998244353;
const int mod = 1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
auto clk = clock();
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
  uniform_int_distribution<int> uid(0, lim - 1);
  return uid(rang);
}
int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const int N = 50000;

bitset<N> a[N];
int b[N];
int fr[N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  int n, m, t;
  cin >> n >> m >> t;
  if (t == 1) {
    std::vector<pii> li;
    for (int i = 0; i < m; ++i) {
      int u;
      cin >> u;
      u--;
      int v;
      cin >> v;
      v--;
      li.pb({u, v});
    }
    reverse(all(li));
    for (int i = 0; i < n; ++i)
      a[i][i] = 1;
    for (auto pi : li)
      a[pi.fi] |= a[pi.se], a[pi.se] |= a[pi.fi];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (!a[j][i]) {
          break;
        } else if (a[j][i] && j == n - 1) {
          a[0].reset();
          a[0][i] = 1;
          string s = "";
          for (auto pi : li) {
            if ((a[0][pi.se] && !a[0][pi.fi])) {
              a[0][pi.fi] = 1;
              s += 'v';
            } else if ((!a[0][pi.se] && a[0][pi.fi])) {
              a[0][pi.se] = 1;
              s += '^';
            } else {
              s += '^';
            }
          }
          reverse(all(s));
          cout << s;
          exit(0);
        }
      }
    }
    cout << -1;
  } else {
    if (n >= 3) {
      std::vector<pii> li;
      for (int i = 0; i < m; ++i) {
        int u;
        cin >> u;
        u--;
        int v;
        cin >> v;
        v--;
        li.pb({u, v});
      }
      reverse(all(li));
      for (int i = 0; i < n; ++i) {
        b[i] = i;
        fr[i] = 1;
      }
      string ss = "";
      for (auto pi : li) {
        if (fr[b[pi.fi]] >= 2) {
          fr[b[pi.fi]]--;
          b[pi.fi] = b[pi.se];
          fr[b[pi.fi]]++;
          ss += "v";
        } else {
          fr[b[pi.se]]--;
          b[pi.se] = b[pi.fi];
          fr[b[pi.se]]++;
          ss += "^";
        }
      }
      reverse(all(ss));
      cout << ss << '\n';
    } else {
      cout << "-1";
    }
  }
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#ifndef print
#define print(...)
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double f80;
#define double long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(x) ((long long)x.size())
#define fr(a, b, c) for (int a = b; a <= c; a++)
#define rep(a, b, c) for (int a = b; a < c; a++)
#define trav(a, x) for (auto &a : x)
#define all(con) con.begin(), con.end()
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
// const int mod=998244353;
const int mod = 1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
auto clk = clock();
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
  uniform_int_distribution<int> uid(0, lim - 1);
  return uid(rang);
}
int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const int N = 50000;

bitset<N> a[N];
int b[N];
int fr[N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  int n, m, t;
  cin >> n >> m >> t;
  if (t == 1) {
    std::vector<pii> li;
    for (int i = 0; i < m; ++i) {
      int u;
      cin >> u;
      u--;
      int v;
      cin >> v;
      v--;
      li.pb({u, v});
    }
    reverse(all(li));
    for (int i = 0; i < n; ++i)
      a[i][i] = 1;
    for (auto pi : li)
      a[pi.fi] |= a[pi.se], a[pi.se] |= a[pi.fi];
    int mi = n + 1;
    int in = -1;
    for (int k = 0; k < n; ++k) {
      if (a[k].count() < mi) {
        mi = a[k].count();
        in = k;
      }
    }
    for (int i = a[in]._Find_first(); i < n; i = a[in]._Find_next(i)) {
      for (int j = 0; j < n; ++j) {
        if (!a[j][i]) {
          break;
        } else if (a[j][i] && j == n - 1) {
          a[0].reset();
          a[0][i] = 1;
          string s = "";
          for (auto pi : li) {
            if ((a[0][pi.se] && !a[0][pi.fi])) {
              a[0][pi.fi] = 1;
              s += 'v';
            } else if ((!a[0][pi.se] && a[0][pi.fi])) {
              a[0][pi.se] = 1;
              s += '^';
            } else {
              s += '^';
            }
          }
          reverse(all(s));
          cout << s;
          exit(0);
        }
      }
    }
    cout << -1;
  } else {
    if (n >= 3) {
      std::vector<pii> li;
      for (int i = 0; i < m; ++i) {
        int u;
        cin >> u;
        u--;
        int v;
        cin >> v;
        v--;
        li.pb({u, v});
      }
      reverse(all(li));
      for (int i = 0; i < n; ++i) {
        b[i] = i;
        fr[i] = 1;
      }
      string ss = "";
      for (auto pi : li) {
        if (fr[b[pi.fi]] >= 2) {
          fr[b[pi.fi]]--;
          b[pi.fi] = b[pi.se];
          fr[b[pi.fi]]++;
          ss += "v";
        } else {
          fr[b[pi.se]]--;
          b[pi.se] = b[pi.fi];
          fr[b[pi.se]]++;
          ss += "^";
        }
      }
      reverse(all(ss));
      cout << ss << '\n';
    } else {
      cout << "-1";
    }
  }
}