結果

問題 No.922 東北きりきざむたん
ユーザー ngtkanangtkana
提出日時 2019-11-08 23:05:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 10,061 bytes
コンパイル時間 3,314 ms
コンパイル使用メモリ 237,812 KB
実行使用メモリ 31,780 KB
最終ジャッジ日時 2024-09-15 02:12:45
合計ジャッジ時間 9,334 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 98 ms
15,616 KB
testcase_10 AC 1,291 ms
7,680 KB
testcase_11 AC 260 ms
12,800 KB
testcase_12 AC 48 ms
18,688 KB
testcase_13 AC 20 ms
7,552 KB
testcase_14 AC 128 ms
23,808 KB
testcase_15 AC 42 ms
19,712 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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[i];}

template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t[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[i] = {i};
    }
  }

  auto collect ()             const {return prt;}
  bool is_root (int x)        const {return find(x) == x;}
  int  size    (int x)        const {return child[prt[x]].size();}
  bool same    (int x, int y) const {return find(x) == find(y);}
  int  find    (int x)        const {return prt[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[x])
    {
      prt[z] = y;
      child[y].push_back(z);
    }
    decltype(child)::value_type{}.swap(child[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[u].emplace_back(v, cost);
      graph[v].emplace_back(u, cost);
    }

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

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

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

    auto weighted_distance (int u, int v) const -> Value {
      return w_depth[u] + w_depth[v] - 2 * w_depth[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[u].emplace_back(v);
    graph[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[i] = vm.issue();
    }
  }
  rep(i, 0, n) {
    cmp[i] = cmp[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[i] = id_seq[cmp[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[u], y = cmp[v];
    int i = id[u], j = id[v];
    if (x == y) {
      internal[x].emplace_back(i, j);
   } else {
      external[x].emplace_back(i);
      external[y].emplace_back(j);
    }
  }

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

    int root = rand(0, n - 1);

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

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

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

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