結果

問題 No.922 東北きりきざむたん
ユーザー ngtkanangtkana
提出日時 2019-11-08 22:59:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 10,265 bytes
コンパイル時間 3,544 ms
コンパイル使用メモリ 229,344 KB
実行使用メモリ 25,536 KB
最終ジャッジ日時 2024-09-15 02:06:31
合計ジャッジ時間 13,259 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define STOPIT
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifdef STOPIT
#define debug(...) 0
#else
#define debug(...)\
do {\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;\
} while (false)
#endif

template <typename T>
auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);}

template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));}

template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {return std::vector<T>(sz);}

template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));}

template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {return t.at(i);}

template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);}

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
  { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

class quick_find
{
  int                           n;
  std::vector<int>              prt;
  std::vector<std::vector<int>> child;

public:
  quick_find (int n) : n(n), prt(n), child(n)
  {
    std::iota(prt.begin(), prt.end(), 0);
    for (int i = 0; i < n; i++) {
      child.at(i) = {i};
    }
  }

  auto collect ()             const {return prt;}
  bool is_root (int x)        const {return find(x) == x;}
  int  size    (int x)        const {return child.at(prt.at(x)).size();}
  bool same    (int x, int y) const {return find(x) == find(y);}
  int  find    (int x)        const {return prt.at(x);}

  // Returns `true` if x and y are newly connected.
  bool unite   (int x, int y)
  {
    if ((x = find(x)) == (y = find(y))) return false;
    if (size(x) > size(y)) std::swap(x, y);
    for (auto z : child.at(x))
    {
      prt.at(z) = y;
      child.at(y).push_back(z);
    }
    decltype(child)::value_type{}.swap(child.at(x));
    return true;
  }
};

template < class Value >
struct vending_machine{
  Value i;
  vending_machine(Value i) : i(i){}
  auto issue() { return i++; }
  auto peek() const { return i; }
};
template < class Value >
auto make_vending_machine(Value i)
  { return vending_machine< Value >(i); }

template < class Value >
class lowest_common_ancestor {
    struct edge {
      int to; Value cost;
      edge(int to, Value cost) : to(to), cost(cost){}
    };
    int n, lg, powlg;
    std::vector< std::vector< edge > > graph;
    std::vector< int >                 depth;
    std::vector< Value >               w_depth;
    std::vector< std::vector< int > >  table;
    std::vector< int > &               tail;

  public:
    using cost_type = Value;
    lowest_common_ancestor(int n) :
      n(n),
      lg(std::log2(n)),
      powlg(std::pow(2, lg)),
      graph(n),
      depth(n, 0),
      w_depth(n, 0),
      table(lg + 1, std::vector< int >(n)),
      tail(table.back())
      {}

    void insert (int u, int v, Value cost = 1) {
      graph.at(u).emplace_back(v, cost);
      graph.at(v).emplace_back(u, cost);
    }

    void build (int root = 0) {
      auto dfs = [&](auto f, int crr, int p) -> void {
        tail.at(crr) = p;
        for (auto const& e : graph.at(crr)) {
          if (e.to == p) continue;
          depth.at(e.to) = depth.at(crr) + 1;
          w_depth.at(e.to) = w_depth.at(crr) + e.cost;
          f(f, e.to, crr);
        }
      };
      dfs(dfs, root, root);
      for (int p = lg; p >= 1; p--) {
        auto & crr = table.at(p);
        auto & nxt = table.at(p - 1);
        for (int i = 0; i < n; i++) {
          nxt.at(i) = crr.at(crr.at(i));
        }
      }
    }

    auto query(int u, int v) const -> int {
      if (depth.at(u) < depth.at(v)) std::swap(u, v);
      auto diff = depth.at(u) - depth.at(v);
      if (diff > 0) {
        int coeff = powlg;
        for (auto const & row : table) {
          if (coeff < diff) {
            u = row.at(u);
            diff -= coeff;
          }
          coeff /= 2;
        }
        u = tail.at(u);
        diff--;
      }
      assert(diff == 0);
      assert(depth.at(u) == depth.at(v));
      if (u == v) return u;
      for (auto const & row : table) {
        auto next_u = row.at(u);
        auto next_v = row.at(v);
        if (next_u != next_v) {
          u = next_u;
          v = next_v;
        }
      }
      assert(u != v);
      u = tail.at(u), v = tail.at(v), assert(u == v);
      return u;
    }

    auto row_distance (int u, int v) const -> int {
      return depth.at(u) + depth.at(v) - 2 * depth.at(query(u, v));
    }

    auto weighted_distance (int u, int v) const -> Value {
      return w_depth.at(u) + w_depth.at(v) - 2 * w_depth.at(query(u, v));
    }
};

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, m, q;
  std::cin >> n >> m >> q;
  auto graph = make_vector< 2, int >(n, 0);
  auto qf = quick_find(n);
  loop(m) {
    int u, v; std::cin >> u >> v;
    u--, v--;
    graph.at(u).emplace_back(v);
    graph.at(v).emplace_back(u);
    qf.unite(u, v);
  }

  vending_machine< int > vm{0};
  std::vector< int > cmp(n);
  rep(i, 0, n) {
    if (qf.is_root(i)) {
      cmp.at(i) = vm.issue();
    }
  }
  rep(i, 0, n) {
    cmp.at(i) = cmp.at(qf.find(i));
  }
  int sz = vm.peek();
  std::vector< vending_machine< int > > id_seq(sz, {0});
  std::vector< int > id(n);
  rep(i, 0, n) {
    id.at(i) = id_seq.at(cmp.at(i)).issue();
  }

  auto internal =  make_vector< 2, std::pair< int, int > >(sz, 0);
  auto external =  make_vector< 2, int >(sz, 0);
  loop(q) {
    int u, v; std::cin >> u >> v;
    u--, v--;
    int x = cmp.at(u), y = cmp.at(v);
    int i = id.at(u), j = id.at(v);
    if (x == y) {
      internal.at(x).emplace_back(i, j);
   } else {
      external.at(x).emplace_back(i);
      external.at(y).emplace_back(j);
    }
  }

  auto sgraph = make_vector< 3, int >(sz, 0, 0);
  rep(i, 0, sz) sgraph.at(i).resize(id_seq.at(i).peek());
  rep(i, 0, n) {
    for (auto j : graph.at(i)) {
      int x = cmp.at(i), y = cmp.at(j);
      assert(x == y);
      at(sgraph, x, id.at(i)).emplace_back(id.at(j));
    }
  }

  auto cal = [&] (auto&& internal, auto&& external, auto&& graph, auto n) -> long long {
    debug(n);
    // debug(internal);
    // debug(external);
    // debug(graph);
    auto lca = lowest_common_ancestor< int >(n);
    rep(i, 0, n) {
      for (auto j : graph.at(i)) {
        lca.insert(i, j);
      }
    }
    lca.build();

    lint ans = 0;
    for (auto pair : internal) {
      int u, v; std::tie(u, v) = pair;
      lint tmp = lca.row_distance(u, v);
      ans += tmp;
    }

    std::vector< lint > sz(n, 0), dp(n);
    for (auto i : external) sz.at(i)++;
    auto dfs = [&] (auto&& dfs, auto crr, auto prv) -> void {
      for (auto nxt : graph.at(crr)) {
        if (nxt == prv) continue;
        dfs(dfs, nxt, crr);
        sz.at(crr) += sz.at(nxt);
        dp.at(crr) += dp.at(nxt) + sz.at(nxt);
      }
    };
    dfs(dfs, 0, 0);
    debug(sz);
    debug(dp);

    auto efs = [&](auto&& efs, auto crr, auto prv) -> void {
      for (auto nxt : graph.at(crr)) {
        if (nxt == prv) continue;
        dp.at(nxt) = dp.at(crr) + external.size() - 2 * sz.at(nxt);
        efs(efs, nxt, crr);
      }
    };
    efs(efs, 0, 0);
    debug(dp);

    ans += *std::min_element(all(dp));
    return ans;
  };

  abort();
  long long ans = 0;
  rep(i, 0, sz) {
    ans += cal(internal.at(i), external.at(i), sgraph.at(i), id_seq.at(i).peek());
  }
  std::cout << ans << std::endl;
  return 0;
}
0