結果
| 問題 |
No.922 東北きりきざむたん
|
| コンテスト | |
| ユーザー |
ngtkana
|
| 提出日時 | 2019-11-08 22:33:25 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 10,314 bytes |
| コンパイル時間 | 3,846 ms |
| コンパイル使用メモリ | 229,980 KB |
| 最終ジャッジ日時 | 2025-01-08 02:38:54 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 6 WA * 9 TLE * 11 |
ソースコード
#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);
}
debug(graph);
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();
}
debug(cmp);
debug(id);
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(internal);
debug(external);
debug(graph);
debug(n);
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);
debug(u, v, tmp);
ans += tmp;
}
std::vector< lint > sz(n, 0), dp(n);
for (auto i : external) sz.at(i) = 1;
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;
};
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;
}
ngtkana