Submission #822936

#TimeUsernameProblemLanguageResultExecution timeMemory
822936arush_aguOne-Way Streets (CEOI17_oneway)C++17
100 / 100
145 ms64580 KiB
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef DEBUG
#include <time.h>
#endif

#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \
  {                                                                            \
    ++recur_depth;                                                             \
    auto x_ = x;                                                               \
    --recur_depth;                                                             \
    cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":"           \
         << __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl;         \
  }
#else
#define dbg(x)
#endif

using namespace std;
using namespace __gnu_pbds;

typedef pair<int, int> ii;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;

typedef vector<bool> vb;

template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
                         tree_order_statistics_node_update>;

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
                                    !is_same<T_container, string>::value,
                                    typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
  os << '{';
  string sep;
  for (const T &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e9;
const ld EPS = 1e-9;

struct Edge {
  int u, v, idx, res = 2;
  bool is_bridge;
};

struct ST {
  const int MAXLG = 22;

  int n;
  vi lg2;
  vvii st;

  ST(int n, vii &a) : n(n) {
    st = vvii(MAXLG, vii(n));
    st[0] = a;

    for (int j = 1; j < MAXLG; j++)
      for (int i = 0; i + (1 << j) <= n; i++)
        st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);

    lg2 = vi(n + 2);
    lg2[1] = 0;
    for (int i = 2; i <= n; i++)
      lg2[i] = lg2[i / 2] + 1;
  }

  int query(int l, int r) {
    if (l > r)
      swap(l, r);
    int k = lg2[r - l + 1];
    return min(st[k][l], st[k][r - (1 << k) + 1]).second;
  }
};

void solve() {
  int n, m;
  cin >> n >> m;
  vector<Edge> eds(m);
  vvii adj(n);
  for (int i = 0; i < m; i++) {
    Edge &e = eds[i];
    cin >> e.u >> e.v;
    e.u--, e.v--;
    e.idx = i;

    adj[e.u].push_back({e.v, i});
    adj[e.v].push_back({e.u, i});
  }

  int p;
  cin >> p;
  vi st(p), en(p);
  for (int i = 0; i < p; i++)
    cin >> st[i] >> en[i], st[i]--, en[i]--;

  int timer = 0;
  vi tin(n), low(n);
  vb vis(n);
  function<void(int, int)> f = [&](int u, int p_e_id) {
    vis[u] = 1;
    tin[u] = low[u] = timer++;

    for (auto [v, i] : adj[u])
      if (i != p_e_id) {
        if (vis[v])
          low[u] = min(low[u], low[v]);
        else {
          f(v, i);
          low[u] = min(low[u], low[v]);
          if (low[v] > tin[u])
            eds[i].is_bridge = 1;
        }
      }
  };
  for (int i = 0; i < n; i++)
    if (!vis[i])
      f(i, -1);

  vis = vb(n);
  vi cc_id(n);
  function<void(int, int)> g = [&](int u, int c_id) {
    vis[u] = 1;
    cc_id[u] = c_id;
    for (auto [v, i] : adj[u])
      if (!vis[v] && !eds[i].is_bridge)
        g(v, c_id);
  };
  int cidx = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i])
      g(i, cidx++);

  vvii tree(cidx);
  for (Edge e : eds)
    if (cc_id[e.u] != cc_id[e.v]) {
      tree[cc_id[e.u]].push_back({cc_id[e.v], e.idx});
      tree[cc_id[e.v]].push_back({cc_id[e.u], e.idx});
    }

  for (int i = 0; i < cidx; i++) {
    sort(all(tree[i]));
    tree[i].erase(unique(all(tree[i])), tree[i].end());
  }

  vi depth(cidx), first_occ(cidx), cnt_child(cidx);
  vii par(cidx, {-1, -1}), euler;
  vis = vb(cidx);
  function<void(int, int)> h = [&](int u, int p) {
    vis[u] = 1;
    first_occ[u] = euler.size();
    euler.push_back({depth[u], u});

    for (auto [v, i] : tree[u])
      if (v != p) {
        par[v] = {u, i};
        cnt_child[u]++;
        depth[v] = depth[u] + 1;
        h(v, u);
        euler.push_back({depth[u], u});
      }
  };
  vi roots;
  for (int i = 0; i < cidx; i++)
    if (!vis[i])
      roots.push_back(i), h(i, -1);

  ST s(euler.size(), euler);

  vi no_st(cidx), no_en(cidx);
  for (int i = 0; i < p; i++)
    if (cc_id[st[i]] != cc_id[en[i]]) {
      int st_cc = cc_id[st[i]], en_cc = cc_id[en[i]];
      int lca = s.query(first_occ[st_cc], first_occ[en_cc]);

      no_st[st_cc]++, no_en[en_cc]++;
      no_st[lca]--, no_en[lca]--;
    }

  function<void(int, int)> d = [&](int u, int p) {
    for (auto [v, i] : tree[u])
      if (v != p) {
        d(v, u);
        no_st[u] += no_st[v];
        no_en[u] += no_en[v];
      }
  };
  for (int root : roots)
    d(root, -1);

  for (int i = 0; i < cidx; i++) {
    auto [p, peid] = par[i];
    if (p < 0)
      continue;
    if (no_st[i]) {
      if (cc_id[eds[peid].u] == i)
        eds[peid].res = 0;
      else
        eds[peid].res = 1;
      continue;
    }
    if (no_en[i]) {
      if (cc_id[eds[peid].u] == i)
        eds[peid].res = 1;
      else
        eds[peid].res = 0;
    }
  }

  for (Edge &e : eds) {
    if (e.res == 2)
      cout << "B";
    else if (e.res == 0)
      cout << "R";
    else
      cout << "L";
  }
  cout << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  clock_t start = clock();

  int test_cases = 1;
  // cin >> test_cases;

  while (test_cases--)
    solve();

#ifdef DEBUG
  cerr << fixed << setprecision(10)
       << "\nTime Taken: " << (double)(clock() - start) / CLOCKS_PER_SEC
       << "s\n";
#endif
  return 0;
}

Compilation message (stderr)

oneway.cpp: In function 'int main()':
oneway.cpp:275:11: warning: unused variable 'start' [-Wunused-variable]
  275 |   clock_t start = clock();
      |           ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...