結果
問題 |
No.3087 University Coloring
|
ユーザー |
|
提出日時 | 2025-04-04 22:03:27 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 84 ms / 2,000 ms |
コード長 | 19,313 bytes |
コンパイル時間 | 1,564 ms |
コンパイル使用メモリ | 140,540 KB |
実行使用メモリ | 8,772 KB |
最終ジャッジ日時 | 2025-04-04 22:04:00 |
合計ジャッジ時間 | 5,704 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include <functional> #include <queue> #include <numeric> #include <istream> #include <unordered_set> #include <stack> #include <fstream> #include <bitset> #include <set> #include <cassert> #include <deque> #include <algorithm> #include <iostream> #include <array> #include <iomanip> #include <map> #include <iterator> #include <optional> #include <string> #include <unordered_map> #include <utility> #include <vector> #include <ostream> #include <type_traits> #ifndef KK2_TEMPLATE_PROCON_HPP #define KK2_TEMPLATE_PROCON_HPP 1 #ifndef KK2_TEMPLATE_CONSTANT_HPP #define KK2_TEMPLATE_CONSTANT_HPP 1 #ifndef KK2_TEMPLATE_TYPE_ALIAS_HPP #define KK2_TEMPLATE_TYPE_ALIAS_HPP 1 using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using pi = std::pair<int, int>; using pl = std::pair<i64, i64>; using pil = std::pair<int, i64>; using pli = std::pair<i64, int>; template <class T> using vc = std::vector<T>; template <class T> using vvc = std::vector<vc<T>>; template <class T> using vvvc = std::vector<vvc<T>>; template <class T> using vvvvc = std::vector<vvvc<T>>; template <class T> using pq = std::priority_queue<T>; template <class T> using pqi = std::priority_queue<T, std::vector<T>, std::greater<T>>; #endif // KK2_TEMPLATE_TYPE_ALIAS_HPP template <class T> constexpr T infty = 0; template <> constexpr int infty<int> = (1 << 30) - 123; template <> constexpr i64 infty<i64> = (1ll << 62) - (1ll << 31); template <> constexpr i128 infty<i128> = (i128(1) << 126) - (i128(1) << 63); template <> constexpr u32 infty<u32> = infty<int>; template <> constexpr u64 infty<u64> = infty<i64>; template <> constexpr u128 infty<u128> = infty<i128>; template <> constexpr double infty<double> = infty<i64>; template <> constexpr long double infty<long double> = infty<i64>; constexpr int mod = 998244353; constexpr int modu = 1e9 + 7; constexpr long double PI = 3.14159265358979323846; #endif // KK2_TEMPLATE_CONSTANT_HPP #ifndef KK2_TEMPLATE_FUNCTION_UTIL_HPP #define KK2_TEMPLATE_FUNCTION_UTIL_HPP 1 namespace kk2 { template <class T, class... Sizes> auto make_vector(int first, Sizes... sizes) { if constexpr (sizeof...(sizes) == 0) { return std::vector<T>(first); } else { return std::vector<decltype(make_vector<T>(sizes...))>(first, make_vector<T>(sizes...)); } } template <class T, class U> void fill_all(std::vector<T> &v, const U &x) { std::fill(std::begin(v), std::end(v), T(x)); } template <class T, class U> void fill_all(std::vector<std::vector<T>> &v, const U &x) { for (auto &u : v) fill_all(u, x); } template <class C> int mysize(const C &c) { return size(c); } // T: comutative monoid template <class T, class U> U all_sum(const std::vector<T> &v, U unit = U()) { U res = unit; for (const auto &x : v) res += x; return res; } template <class T, class U> U all_sum(const std::vector<std::vector<T>> &v, U unit = U()) { U res = U(); for (const auto &u : v) res += all_sum(u, unit); return res; } // T: commutative monoid, F: (U, T) -> U template <class T, class U, class F> U all_prod(const std::vector<T> &v, U init, const F &f, U = U()) { U res = init; for (const auto &x : v) res = f(res, x); return res; } template <class T, class U, class F> U all_prod(const std::vector<std::vector<T>> &v, U init, const F &f, U unit) { U res = init; for (const auto &u : v) res = f(res, all_prod(u, unit, f, unit)); return res; } template <class T> T all_min(const std::vector<T> &v) { if (v.empty()) return T(); T res = v[0]; for (const auto &x : v) res = res > x ? x : res; return res; } template <class T> T all_min(const std::vector<std::vector<T>> &v) { T res{}; bool first = true; for (const auto &u : v) { if (u.empty()) continue; if (first) { res = all_min(u); first = false; } else { T tmp = all_min(u); res = res > tmp ? tmp : res; } } return res; } template <class T> T all_max(const std::vector<T> &v) { if (v.empty()) return T(); T res = v[0]; for (const auto &x : v) res = res < x ? x : res; return res; } template <class T> T all_max(const std::vector<std::vector<T>> &v) { T res{}; bool first = true; for (const auto &u : v) { if (u.empty()) continue; if (first) { res = all_max(u); first = false; } else { T tmp = all_max(u); res = res < tmp ? tmp : res; } } return res; } } // namespace kk2 #endif // KK2_TEMPLATE_FUNCTION_UTIL_HPP #ifndef KK2_TEMPLATE_IO_UTIL_HPP #define KK2_TEMPLATE_IO_UTIL_HPP 1 #ifndef KK2_TYPE_TRAITS_TYPE_TRAITS_HPP #define KK2_TYPE_TRAITS_TYPE_TRAITS_HPP 1 namespace kk2 { #ifndef _MSC_VER template <typename T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value or std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <typename T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value or std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <typename T> using is_integral = typename std::conditional<std::is_integral<T>::value or is_signed_int128<T>::value or is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <typename T> using is_signed = typename std::conditional<std::is_signed<T>::value or is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <typename T> using is_unsigned = typename std::conditional<std::is_unsigned<T>::value or is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <typename T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <typename T> using to_unsigned = typename std::conditional<is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <typename T> using is_integral = std::enable_if_t<std::is_integral<T>::value>; template <typename T> using is_signed = std::enable_if_t<std::is_signed<T>::value>; template <typename T> using is_unsigned = std::enable_if_t<std::is_unsigned<T>::value>; template <typename T> using to_unsigned = std::make_unsigned<T>; #endif // _MSC_VER template <typename T> using is_integral_t = std::enable_if_t<is_integral<T>::value>; template <typename T> using is_signed_t = std::enable_if_t<is_signed<T>::value>; template <typename T> using is_unsigned_t = std::enable_if_t<is_unsigned<T>::value>; template <typename T> using is_function_pointer = typename std::conditional<std::is_pointer_v<T> && std::is_function_v<std::remove_pointer_t<T>>, std::true_type, std::false_type>::type; template <typename T, std::enable_if_t<is_function_pointer<T>::value> * = nullptr> struct is_two_args_function_pointer : std::false_type {}; template <typename R, typename T1, typename T2> struct is_two_args_function_pointer<R (*)(T1, T2)> : std::true_type {}; template <typename T> using is_two_args_function_pointer_t = std::enable_if_t<is_two_args_function_pointer<T>::value>; namespace type_traits { struct istream_tag {}; struct ostream_tag {}; } // namespace type_traits template <typename T> using is_standard_istream = typename std::conditional<std::is_same<T, std::istream>::value || std::is_same<T, std::ifstream>::value, std::true_type, std::false_type>::type; template <typename T> using is_standard_ostream = typename std::conditional<std::is_same<T, std::ostream>::value || std::is_same<T, std::ofstream>::value, std::true_type, std::false_type>::type; template <typename T> using is_user_defined_istream = std::is_base_of<type_traits::istream_tag, T>; template <typename T> using is_user_defined_ostream = std::is_base_of<type_traits::ostream_tag, T>; template <typename T> using is_istream = typename std::conditional<is_standard_istream<T>::value || is_user_defined_istream<T>::value, std::true_type, std::false_type>::type; template <typename T> using is_ostream = typename std::conditional<is_standard_ostream<T>::value || is_user_defined_ostream<T>::value, std::true_type, std::false_type>::type; template <typename T> using is_istream_t = std::enable_if_t<is_istream<T>::value>; template <typename T> using is_ostream_t = std::enable_if_t<is_ostream<T>::value>; } // namespace kk2 #endif // KK2_TYPE_TRAITS_TYPE_TRAITS_HPP // なんかoj verifyはプロトタイプ宣言が落ちる namespace impl { struct read { template <class IStream, class T> inline static void all_read(IStream &is, T &x) { is >> x; } template <class IStream, class T, class U> inline static void all_read(IStream &is, std::pair<T, U> &p) { all_read(is, p.first); all_read(is, p.second); } template <class IStream, class T> inline static void all_read(IStream &is, std::vector<T> &v) { for (T &x : v) all_read(is, x); } template <class IStream, class T, size_t F> inline static void all_read(IStream &is, std::array<T, F> &a) { for (T &x : a) all_read(is, x); } }; struct write { template <class OStream, class T> inline static void all_write(OStream &os, const T &x) { os << x; } template <class OStream, class T, class U> inline static void all_write(OStream &os, const std::pair<T, U> &p) { all_write(os, p.first); all_write(os, ' '); all_write(os, p.second); } template <class OStream, class T> inline static void all_write(OStream &os, const std::vector<T> &v) { for (int i = 0; i < (int)v.size(); ++i) { if (i) all_write(os, ' '); all_write(os, v[i]); } } template <class OStream, class T, size_t F> inline static void all_write(OStream &os, const std::array<T, F> &a) { for (int i = 0; i < (int)F; ++i) { if (i) all_write(os, ' '); all_write(os, a[i]); } } }; } // namespace impl template <class IStream, class T, class U, kk2::is_istream_t<IStream> * = nullptr> IStream &operator>>(IStream &is, std::pair<T, U> &p) { impl::read::all_read(is, p); return is; } template <class IStream, class T, kk2::is_istream_t<IStream> * = nullptr> IStream &operator>>(IStream &is, std::vector<T> &v) { impl::read::all_read(is, v); return is; } template <class IStream, class T, size_t F, kk2::is_istream_t<IStream> * = nullptr> IStream &operator>>(IStream &is, std::array<T, F> &a) { impl::read::all_read(is, a); return is; } template <class OStream, class T, class U, kk2::is_ostream_t<OStream> * = nullptr> OStream &operator<<(OStream &os, const std::pair<T, U> &p) { impl::write::all_write(os, p); return os; } template <class OStream, class T, kk2::is_ostream_t<OStream> * = nullptr> OStream &operator<<(OStream &os, const std::vector<T> &v) { impl::write::all_write(os, v); return os; } template <class OStream, class T, size_t F, kk2::is_ostream_t<OStream> * = nullptr> OStream &operator<<(OStream &os, const std::array<T, F> &a) { impl::write::all_write(os, a); return os; } #endif // KK2_TEMPLATE_IO_UTIL_HPP #ifndef KK2_TEMPLATE_MACROS_HPP #define KK2_TEMPLATE_MACROS_HPP 1 #define rep1(a) for (long long _ = 0; _ < (long long)(a); ++_) #define rep2(i, a) for (long long i = 0; i < (long long)(a); ++i) #define rep3(i, a, b) for (long long i = (a); i < (long long)(b); ++i) #define repi2(i, a) for (long long i = (a) - 1; i >= 0; --i) #define repi3(i, a, b) for (long long i = (a) - 1; i >= (long long)(b); --i) #define overload3(a, b, c, d, ...) d #define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi(...) overload3(__VA_ARGS__, repi3, repi2, rep1)(__VA_ARGS__) #define fi first #define se second #define all(p) begin(p), end(p) #endif // KK2_TEMPLATE_MACROS_HPP struct FastIOSetUp { FastIOSetUp() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } fast_io_set_up; auto &kin = std::cin; auto &kout = std::cout; auto (*kendl)(std::ostream &) = std::endl<char, std::char_traits<char>>; void Yes(bool b = 1) { kout << (b ? "Yes\n" : "No\n"); } void No(bool b = 1) { kout << (b ? "No\n" : "Yes\n"); } void YES(bool b = 1) { kout << (b ? "YES\n" : "NO\n"); } void NO(bool b = 1) { kout << (b ? "NO\n" : "YES\n"); } void yes(bool b = 1) { kout << (b ? "yes\n" : "no\n"); } void no(bool b = 1) { kout << (b ? "no\n" : "yes\n"); } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } std::istream &operator>>(std::istream &is, u128 &x) { std::string s; is >> s; x = 0; for (char c : s) { assert('0' <= c && c <= '9'); x = x * 10 + c - '0'; } return is; } std::istream &operator>>(std::istream &is, i128 &x) { std::string s; is >> s; bool neg = s[0] == '-'; x = 0; for (int i = neg; i < (int)s.size(); i++) { assert('0' <= s[i] && s[i] <= '9'); x = x * 10 + s[i] - '0'; } if (neg) x = -x; return is; } std::ostream &operator<<(std::ostream &os, u128 x) { if (x == 0) return os << '0'; std::string s; while (x) { s.push_back('0' + x % 10); x /= 10; } std::reverse(s.begin(), s.end()); return os << s; } std::ostream &operator<<(std::ostream &os, i128 x) { if (x == 0) return os << '0'; if (x < 0) { os << '-'; x = -x; } std::string s; while (x) { s.push_back('0' + x % 10); x /= 10; } std::reverse(s.begin(), s.end()); return os << s; } #endif // KK2_TEMPLATE_PROCON_HPP // #include <kk2/template/debug.hpp> #ifndef KK2_UNIONFIND_UNIONFIND_HPP #define KK2_UNIONFIND_UNIONFIND_HPP 1 namespace kk2 { struct UnionFind { std::vector<int> d; UnionFind(int n = 0) : d(n, -1) {} bool same(int x, int y) { return find(x) == find(y); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return false; if (d[x] > d[y]) std::swap(x, y); d[x] += d[y]; d[y] = x; return true; } int find(int x) { if (d[x] < 0) return x; return d[x] = find(d[x]); } int size(int x) { return -d[find(x)]; } }; } // namespace kk2 #endif // KK2_UNIONFIND_UNIONFIND_HPP #ifndef KK2_GRAPH_EDGE_HPP #define KK2_GRAPH_EDGE_HPP 1 namespace kk2 { namespace graph { struct empty {}; template <class T> struct _Edge { int from, to, id; T cost; _Edge(int to_, T cost_, int from_ = -1, int id_ = -1) : from(from_), to(to_), id(id_), cost(cost_) {} _Edge() : from(-1), to(-1), id(-1), cost() {} operator int() const { return to; } _Edge rev() const { return _Edge(from, cost, to, id); } template <class OStream, is_ostream_t<OStream> * = nullptr> void debug_output(OStream &os) const { os << '(' << id << ", " << from << "->" << to; if constexpr (!std::is_same_v<T, empty>) os << ":" << cost; os << ')'; } }; template <class T> struct _Edges : public std::vector<_Edge<T>> { using std::vector<_Edge<T>>::vector; template <class IStream, is_istream_t<IStream> * = nullptr> _Edges &input(IStream &is, bool is_one_indexed = false) { for (int i = 0; i < (int)this->size(); i++) { int u, v; T w{}; is >> u >> v; if (is_one_indexed) --u, --v; if constexpr (!std::is_same_v<T, empty>) is >> w; (*this)[i] = _Edge<T>(v, w, u, i); } return *this; } template <class IStream, is_istream_t<IStream> * = nullptr> friend _Edges &input(_Edges &edges, IStream &is, bool is_one_indexed = false) { return edges.input(is, is_one_indexed); } template <class OStream, is_ostream_t<OStream> * = nullptr> void debug_output(OStream &os) const { os << '['; for (int i = 0; i < (int)this->size(); i++) { if (i) os << ", "; (*this)[i].debug_output(os); } os << ']'; } _Edges &add_edge(int from, int to, T cost = T{}) { this->emplace_back(to, cost, from, this->size()); return *this; } friend _Edges &add_edge(_Edges &edges, int from, int to, T cost = T{}) { edges.emplace_back(to, cost, from, edges.size()); return edges; } }; template <class T> struct _pair { T cost; int id; _pair(T cost_, int id_) : cost(cost_), id(id_) {} _pair() : cost(), id(-1) {} operator bool() const { return id != -1; } template <class OStream, is_ostream_t<OStream> * = nullptr> friend OStream &operator<<(OStream &os, const _pair &p) { if constexpr (std::is_same_v<T, empty>) return os; else return os << p.cost; } }; template <class T> using _pairs = std::vector<_pair<T>>; } // namespace graph template <typename T> using WEdge = graph::_Edge<T>; template <typename T> using WEdges = graph::_Edges<T>; using Edge = graph::_Edge<graph::empty>; using Edges = graph::_Edges<graph::empty>; } // namespace kk2 #endif // KK2_GRAPH_EDGE_HPP using namespace std; void solve() { /* dfsで全部いけますか? */ int n, m; kin >> n >> m; kk2::WEdges<i64> edges(m); edges.input(kin, 1); sort(all(edges), [](const auto &a, const auto &b) { return a.cost > b.cost; }); i64 res = 0; kk2::UnionFind uf(n); for (auto e : edges) { if (uf.unite(e.from, e.to)) res += e.cost; } kout << res * 2 << kendl; } int main() { int t = 1; // kin >> t; rep (t) solve(); return 0; } // Author: kk2 // converted by https://github.com/kk2a/cpp-bundle // 2025-04-04 22:03:23