結果
問題 | No.2642 Don't cut line! |
ユーザー | Forested |
提出日時 | 2024-02-19 21:46:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 118 ms / 4,000 ms |
コード長 | 13,662 bytes |
コンパイル時間 | 2,949 ms |
コンパイル使用メモリ | 232,940 KB |
実行使用メモリ | 38,492 KB |
最終ジャッジ日時 | 2024-09-29 03:31:47 |
合計ジャッジ時間 | 6,909 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 112 ms
36,700 KB |
testcase_02 | AC | 118 ms
38,492 KB |
testcase_03 | AC | 114 ms
36,976 KB |
testcase_04 | AC | 110 ms
35,992 KB |
testcase_05 | AC | 114 ms
37,808 KB |
testcase_06 | AC | 68 ms
6,820 KB |
testcase_07 | AC | 68 ms
6,816 KB |
testcase_08 | AC | 66 ms
6,820 KB |
testcase_09 | AC | 66 ms
6,820 KB |
testcase_10 | AC | 65 ms
6,820 KB |
testcase_11 | AC | 67 ms
6,816 KB |
testcase_12 | AC | 71 ms
6,820 KB |
testcase_13 | AC | 66 ms
6,816 KB |
testcase_14 | AC | 65 ms
6,816 KB |
testcase_15 | AC | 69 ms
6,816 KB |
testcase_16 | AC | 52 ms
6,820 KB |
testcase_17 | AC | 91 ms
33,576 KB |
testcase_18 | AC | 102 ms
33,204 KB |
testcase_19 | AC | 72 ms
26,112 KB |
testcase_20 | AC | 51 ms
14,068 KB |
testcase_21 | AC | 44 ms
6,820 KB |
testcase_22 | AC | 39 ms
6,820 KB |
testcase_23 | AC | 116 ms
37,080 KB |
testcase_24 | AC | 52 ms
15,444 KB |
testcase_25 | AC | 57 ms
14,048 KB |
testcase_26 | AC | 71 ms
8,648 KB |
testcase_27 | AC | 68 ms
25,796 KB |
testcase_28 | AC | 101 ms
34,360 KB |
testcase_29 | AC | 67 ms
10,252 KB |
testcase_30 | AC | 62 ms
12,652 KB |
testcase_31 | AC | 82 ms
28,684 KB |
testcase_32 | AC | 65 ms
11,852 KB |
testcase_33 | AC | 2 ms
6,820 KB |
testcase_34 | AC | 2 ms
6,816 KB |
testcase_35 | AC | 2 ms
6,820 KB |
ソースコード
#ifndef LOCAL #define FAST_IO #endif // ============ #include <bits/stdc++.h> #define OVERRIDE(a, b, c, d, ...) d #define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i) #define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i) #define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__) #define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i) #define PER3(i, m, n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i) #define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__) #define ALL(x) begin(x), end(x) #define LEN(x) (i32)(x.size()) using namespace std; using u32 = unsigned int; using u64 = unsigned long long; using i32 = signed int; using i64 = signed long long; using f64 = double; using f80 = long double; using pi = pair<i32, i32>; using pl = pair<i64, i64>; template <typename T> using V = vector<T>; template <typename T> using VV = V<V<T>>; template <typename T> using VVV = V<V<V<T>>>; template <typename T> using VVVV = V<V<V<V<T>>>>; template <typename T> using PQR = priority_queue<T, V<T>, greater<T>>; template <typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template <typename T> i32 lob(const V<T> &arr, const T &v) { return (i32)(lower_bound(ALL(arr), v) - arr.begin()); } template <typename T> i32 upb(const V<T> &arr, const T &v) { return (i32)(upper_bound(ALL(arr), v) - arr.begin()); } template <typename T> V<i32> argsort(const V<T> &arr) { V<i32> ret(arr.size()); iota(ALL(ret), 0); sort(ALL(ret), [&](i32 i, i32 j) -> bool { if (arr[i] == arr[j]) { return i < j; } else { return arr[i] < arr[j]; } }); return ret; } #ifdef INT128 using u128 = __uint128_t; using i128 = __int128_t; #endif [[maybe_unused]] constexpr i32 INF = 1000000100; [[maybe_unused]] constexpr i64 INF64 = 3000000000000000100; struct SetUpIO { SetUpIO() { #ifdef FAST_IO ios::sync_with_stdio(false); cin.tie(nullptr); #endif cout << fixed << setprecision(15); } } set_up_io; void scan(char &x) { cin >> x; } void scan(u32 &x) { cin >> x; } void scan(u64 &x) { cin >> x; } void scan(i32 &x) { cin >> x; } void scan(i64 &x) { cin >> x; } void scan(string &x) { cin >> x; } template <typename T> void scan(V<T> &x) { for (T &ele : x) { scan(ele); } } void read() {} template <typename Head, typename... Tail> void read(Head &head, Tail &...tail) { scan(head); read(tail...); } #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__); #define U32(...) \ u32 __VA_ARGS__; \ read(__VA_ARGS__); #define U64(...) \ u64 __VA_ARGS__; \ read(__VA_ARGS__); #define I32(...) \ i32 __VA_ARGS__; \ read(__VA_ARGS__); #define I64(...) \ i64 __VA_ARGS__; \ read(__VA_ARGS__); #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__); #define VEC(type, name, size) \ V<type> name(size); \ read(name); #define VVEC(type, name, size1, size2) \ VV<type> name(size1, V<type>(size2)); \ read(name); // ============ #ifdef DEBUGF #else #define DBG(...) (void)0 #endif // ============ #include <cassert> #include <utility> #include <vector> class UnionFind { std::vector<int> par; int _root(int v) { if (par[v] < 0) { return v; } else { return par[v] = _root(par[v]); } } public: UnionFind(int n) : par(n, -1) {} int root(int v) { assert(v >= 0 && v < (int)par.size()); return _root(v); } int size(int v) { assert(v >= 0 && v < (int)par.size()); return -par[_root(v)]; } bool unite(int u, int v) { assert(u >= 0 && u < (int)par.size() && v >= 0 && v < (int)par.size()); u = _root(u); v = _root(v); if (u == v) { return false; } if (par[u] < par[v]) { std::swap(u, v); } par[v] += par[u]; par[u] = v; return true; } bool same(int u, int v) { assert(u >= 0 && u < (int)par.size() && v >= 0 && v < (int)par.size()); return _root(u) == _root(v); } }; // ============ // ============ #include <algorithm> #include <utility> // ============ #include <iostream> #include <cassert> #include <vector> template <typename T> struct Edge { using W = T; int from, to, id; W weight; Edge<T> rev() const { return Edge<T>{to, from, id, weight}; } }; template <typename T> void debug(const Edge<T> &e) { std::cerr << e.from << " -> " << e.to << " id = " << e.id << std::cerr << " weight = "; debug(e.weight); } template <typename T = int, bool DIR = false> class Graph { public: using E = Edge<T>; using W = T; static constexpr bool DIRECTED = DIR; struct Adjacency { using Iter = typename std::vector<E>::iterator; Iter be, en; Iter begin() const { return be; } Iter end() const { return en; } int size() const { return (int)std::distance(be, en); } E &operator[](int idx) const { return be[idx]; } }; struct ConstAdjacency { using Iter = typename std::vector<E>::const_iterator; Iter be, en; Iter begin() const { return be; } Iter end() const { return en; } int size() const { return (int)std::distance(be, en); } const E &operator[](int idx) const { return be[idx]; } }; private: int n, m; std::vector<E> edges, csr; std::vector<int> sep; bool built; public: Graph(int n) : n(n), m(0), built(false) {} int v() const { return n; } int e() const { return m; } int add_vertex() { return n++; } void add_edge(int from, int to, W weight = 1) { assert(0 <= from && from < n && 0 <= to && to < n); edges.emplace_back(E{from, to, m++, weight}); } void build() { sep.assign(n + 1, 0); csr.resize(DIRECTED ? m : 2 * m); for (const E &e : edges) { ++sep[e.from + 1]; if (!DIRECTED) { ++sep[e.to + 1]; } } for (int i = 0; i < n; ++i) { sep[i + 1] += sep[i]; } std::vector<int> c = sep; for (const E &e : edges) { csr[c[e.from]++] = e; if (!DIRECTED) { csr[c[e.to]++] = e.rev(); } } built = true; } Adjacency operator[](int v) { assert(built && 0 <= v && v < n); return Adjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]}; } ConstAdjacency operator[](int v) const { assert(built && 0 <= v && v < n); return ConstAdjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]}; } }; // ============ class HeavyLightDecomposition { public: std::vector<int> siz, par, hea, in, out, dep, rev; private: template <typename T, bool DIR> void dfs1(Graph<T, DIR> &g, int v) { if (g[v].size() >= 1 && g[v][0].to == par[v]) { std::swap(g[v][0], g[v][g[v].size() - 1]); } for (Edge<T> &e : g[v]) { if (e.to != par[v]) { par[e.to] = v; dfs1(g, e.to); siz[v] += siz[e.to]; if (siz[e.to] > siz[g[v][0].to]) { std::swap(g[v][0], e); } } } } template <typename T, bool DIR> void dfs2(const Graph<T, DIR> &g, int v, int &t) { in[v] = t; rev[t++] = v; for (const Edge<T> &e : g[v]) { if (e.to == par[v]) { continue; } if (e.to == g[v][0].to) { hea[e.to] = hea[v]; } else { hea[e.to] = e.to; } dep[e.to] = dep[v] + 1; dfs2(g, e.to, t); } out[v] = t; } public: template <typename T, bool DIR> HeavyLightDecomposition(Graph<T, DIR> &g, int root = 0) : siz(g.v(), 1), par(g.v(), root), hea(g.v(), root), in(g.v(), 0), out(g.v(), 0), dep(g.v(), 0), rev(g.v(), 0) { assert(0 <= root && root < g.v()); dfs1(g, root); int t = 0; dfs2(g, root, t); } // par^k int la(int v, int k) const { assert(0 <= v && v < (int)dep.size()); assert(k >= 0); if (k > dep[v]) { return -1; } while (true) { int u = hea[v]; if (in[u] + k <= in[v]) { return rev[in[v] - k]; } k -= in[v] - in[u] + 1; v = par[u]; } return 0; } int lca(int u, int v) const { assert(0 <= u && u < (int)dep.size()); assert(0 <= v && v < (int)dep.size()); while (u != v) { if (in[u] > in[v]) { std::swap(u, v); } if (hea[u] == hea[v]) { v = u; } else { v = par[hea[v]]; } } return u; } int dist(int u, int v) const { assert(0 <= u && u < (int)dep.size()); assert(0 <= v && v < (int)dep.size()); return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } int jump(int u, int v, int k) const { assert(0 <= u && u < (int)dep.size()); assert(0 <= v && v < (int)dep.size()); assert(k >= 0); int l = lca(u, v); int dis = dep[u] + dep[v] - 2 * dep[l]; if (k > dis) { return -1; } if (k <= dep[u] - dep[l]) { return la(u, k); } else { return la(v, dis - k); } } int meet(int u, int v, int w) const { return lca(u, v) ^ lca(v, w) ^ lca(w, u); } std::vector<std::pair<int, int>> path(int u, int v, bool edge) const { assert(0 <= u && u < (int)dep.size()); assert(0 <= v && v < (int)dep.size()); std::vector<std::pair<int, int>> fromu, fromv; bool rev = false; while (true) { if (u == v && edge) { break; } if (in[u] > in[v]) { std::swap(u, v); std::swap(fromu, fromv); rev ^= true; } if (hea[u] == hea[v]) { fromv.emplace_back(in[v], in[u] + (int)edge); v = u; break; } else { fromv.emplace_back(in[v], in[hea[v]]); v = par[hea[v]]; } } if (rev) { std::swap(fromu, fromv); } std::reverse(fromv.begin(), fromv.end()); fromu.reserve(fromv.size()); for (auto [x, y] : fromv) { fromu.emplace_back(y, x); } return fromu; } }; // ============ // ============ #include <algorithm> #include <cassert> #include <utility> #include <vector> #include <functional> template <typename T, typename F = std::less<T>> class SparseTable { std::vector<std::vector<T>> table; int s; const F f; int log2(int n) const { return 31 - __builtin_clz(n); } T min2(const T &x, const T &y) const { if (f(x, y)) { return x; } else { return y; } } public: SparseTable(std::vector<T> arr, const F &f = F()) : s((int) arr.size()), f(f) { if (s == 0) { return; } int m = log2(s); table.resize(m + 1); table[0] = std::move(arr); for (int i = 1; i <= m; ++i) { int w = 1 << i; table[i].resize(s - w + 1); for (int j = 0; j < (int) table[i].size(); ++j) { table[i][j] = min2(table[i - 1][j], table[i - 1][j + (w >> 1)]); } } } int size() const { return s; } // not empty T min(int l, int r) const { assert(l >= 0 && l < r && r <= s); int m = log2(r - l); return min2(table[m][l], table[m][r - (1 << m)]); } }; // ============ void solve() { I32(n, m); I64(c); V<i32> u(m), v(m); V<i64> w(m), p(m); REP(i, m) { read(u[i], v[i], w[i], p[i]); --u[i]; --v[i]; } V<i32> mst(m, 0); UnionFind uf(n); for (i32 i : argsort(w)) { if (uf.unite(u[i], v[i])) { mst[i] = 1; } } i64 mst_cost = 0; REP(i, m) { if (mst[i]) { mst_cost += w[i]; } } if (mst_cost > c) { cout << -1 << '\n'; return; } Graph<i64> g(n); REP(i, m) { if (mst[i]) { g.add_edge(u[i], v[i], w[i]); } } g.build(); HeavyLightDecomposition hld(g); V<i64> wt(n, 0); REP(v, n) { for (auto e : g[v]) { if (hld.par[e.to] == v) { wt[hld.in[e.to]] = e.weight; } } } SparseTable<i64, greater<>> sp(wt); i64 ans = 0; REP(i, m) { i64 weight_max = 0; for (auto [l, r] : hld.path(u[i], v[i], true)) { if (l > r) { swap(l, r); } ++r; chmax(weight_max, sp.min(l, r)); } i64 tree_weight = mst_cost - weight_max + w[i]; if (tree_weight <= c) { chmax(ans, p[i]); } } cout << ans << '\n'; } int main() { i32 t = 1; // cin >> t; while (t--) { solve(); } }