Submission #821248

# Submission time Handle Problem Language Result Execution time Memory
821248 2023-08-11T08:21:42 Z arush_agu One-Way Streets (CEOI17_oneway) C++17
0 / 100
1 ms 592 KB
#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 ST {
  const int MAXLG = 22;

  int n;
  vvii st;
  vi lg2;

  ST(vii a) : n(a.size()) {
    lg2 = vi(n + 10);
    lg2[1] = 0;
    for (int i = 2; i <= n; i++)
      lg2[i] = lg2[i / 2] + 1;

    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))]);
  }

  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;
  }
};

struct DSU {
  int n;
  vi par, sz;
  int no_cc;

  DSU(int n) : n(n), par(vi(n, -1)), sz(vi(n, 1)), no_cc(n) {}

  int find(int x) {
    if (par[x] == -1)
      return x;
    return par[x] = find(par[x]);
  }

  void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v)
      return;

    if (sz[u] < sz[v])
      swap(u, v);

    par[v] = u;
    sz[u] += sz[v];
    no_cc--;
  }
};

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

void solve() {
  int n, m;
  cin >> n >> m;
  vvii adj(n);
  vector<Edge> eds(m);
  int i_ = 0;
  for (auto &e : eds) {
    cin >> e.u >> e.v;
    e.u--, e.v--;
    e.idx = i_++;
    e.res = 2;

    adj[e.u].push_back({e.v, e.idx});
    adj[e.v].push_back({e.u, e.idx});
  }
  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, e_id] : adj[u])
      if (e_id != p_e_id) {
        if (vis[v])
          low[u] = min(low[u], low[v]);
        else {
          f(v, e_id);
          low[u] = min(low[u], low[v]);
          if (low[v] > tin[u])
            eds[e_id].is_bridge = 1;
        }
      }
  };
  for (int i = 0; i < n; i++)
    if (!vis[i])
      f(i, -1);

  DSU d(n);
  for (auto e : eds)
    if (!e.is_bridge)
      d.merge(e.u, e.v);

  int new_idx = 0;
  vi new_idxs(n, -1);
  auto get_idx = [&](int x) {
    x = d.find(x);
    if (new_idxs[x] != -1)
      return new_idxs[x];
    return new_idxs[x] = new_idx++;
  };

  int nn = d.no_cc;
  if (nn == 1) {
    cout << string(m, 'B') << "\n";
    return;
  }

  vvii tree(nn);
  for (auto e : eds)
    if (e.is_bridge)
      tree[get_idx(e.u)].push_back({get_idx(e.v), e.idx}),
          tree[get_idx(e.v)].push_back({get_idx(e.u), e.idx});

  vi depth(nn), first_occ(nn, -1), cnt_child(nn);
  vii euler, par(nn, {-1, -1});
  vb nvis(nn);
  function<void(int, int)> g = [&](int u, int p) {
    nvis[u] = 1;
    first_occ[u] = euler.size();
    euler.emplace_back(depth[u], u);

    for (auto [v, e] : tree[u])
      if (v != p) {
        par[v] = {u, e};
        cnt_child[u]++;
        depth[v] = depth[u] + 1;
        g(v, u);
        euler.emplace_back(depth[u], u);
      }
  };
  int root = 0;
  g(root, -1);

  // cerr << euler << "\n";
  // cerr << first_occ << "\n";

  ST lc(euler);

  for (int i = 0; i < nn; i++)
    assert(nvis[i]);

  vi no_st(nn), no_en(nn);

  for (int i = 0; i < p; i++) {
    int u = get_idx(st[i]), v = get_idx(en[i]);
    if (u == v)
      continue;
    int lca = lc.query(first_occ[u], first_occ[v]);
    no_st[u]++, no_en[v]++;
    no_st[lca]--, no_en[lca]--;
  }

  queue<int> q;
  for (int i = 0; i < nn; i++)
    if (cnt_child[i] == 0 && i != root)
      q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();

    // cerr << "U: " << u << '\n';
    // cerr << "\t" << no_st[u] << " " << no_en[u] << "\n";

    auto [p, p_e_id] = par[u];
    if (p < 0)
      break;

    assert(no_st[u] == 0 || no_en[u] == 0);
    if (no_st[u] > 0) {
      if (get_idx(eds[p_e_id].u) == u)
        eds[p_e_id].res = 0;
      else
        eds[p_e_id].res = 1;
    }
    if (no_en[u] > 0) {
      if (get_idx(eds[p_e_id].u) == u)
        eds[p_e_id].res = 1;
      else
        eds[p_e_id].res = 0;
    }

    no_st[p] += no_st[u];
    no_en[p] += no_en[u];

    if (--cnt_child[p] == 0)
      q.push(p);
  }

  for (int i = 0; i < m; i++) {
    if (eds[i].res == 0)
      cout << "R";
    else if (eds[i].res == 1)
      cout << "L";
    else
      cout << "B";
  }
  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

oneway.cpp: In function 'int main()':
oneway.cpp:311:11: warning: unused variable 'start' [-Wunused-variable]
  311 |   clock_t start = clock();
      |           ^~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -