#line 1 "a.cpp" #define _GLIBCXX_DEBUG #line 1 "/home/nok0/documents/programming/library/atcoder/dsu.hpp" #include #include #include namespace atcoder { // Implement (union by size) + (path compression) // Reference: // Zvi Galil and Giuseppe F. Italiano, // Data structures and algorithms for disjoint set union problems struct dsu { public: dsu() : _n(0) {} explicit dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector> groups() { std::vector leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector& v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector parent_or_size; }; } // namespace atcoder #line 2 "/home/nok0/documents/programming/library/template/header.hpp" #include #line 3 "/home/nok0/documents/programming/library/graph/graph.hpp" #pragma region graph template class graph { public: struct edge { public: int from, to; cost_type cost; int id; edge() = default; edge(int from_, int to_, cost_type cost_ = 1, int id_ = -1) : from(from_), to(to_), cost(cost_), id(id_) {} bool operator<(const edge &a) const { return cost < a.cost; } bool operator>(const edge &a) const { return cost > a.cost; } friend std::ostream &operator<<(std::ostream &s, const edge &a) { s << '(' << a.from << " -> " << a.to << "), cost: " << a.cost << ", id: " << a.id; return s; } }; private: std::vector> edges; int next_edge_id = 0; public: inline const std::vector &operator[](int k) const { return edges[k]; } inline std::vector &operator[](int k) { return edges[k]; } int size() const { return int(edges.size()); } void resize(const int n) { edges.resize(n); } int edge_count() const { return next_edge_id; } friend std::ostream &operator<<(std::ostream &s, const graph &g) { for(const auto &adj : g.edges) for(const auto &ed : adj) s << ed << '\n'; return s; } graph() = default; graph(int n) : edges(n) {} graph(int n, int e, bool weight = 0, bool directed = 0, int idx = 1) : edges(n) { input(e, weight, directed, idx); } const cost_type INF = std::numeric_limits::max() / 3; void input(int e = -1, bool weight = false, bool directed = false, int idx = 1) { if(e == -1) e = size() - 1; while(e--) { int u, v; std::cin >> u >> v; cost_type cost = 1; if(weight) std::cin >> cost; add_edge(u, v, cost, directed, idx); } } inline int add_edge(int u, int v, cost_type cost = 1, bool directed = false, int idx = 0) { u -= idx, v -= idx; edges[u].emplace_back(u, v, cost, next_edge_id); if(!directed && u != v) edges[v].emplace_back(v, u, cost, next_edge_id); return next_edge_id++; } // Ο(V+E) std::vector bfs(int s) const { std::vector dist(size(), INF); std::queue que; dist[s] = 0; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(auto &e : edges[v]) { if(dist[e.to] != INF) continue; dist[e.to] = dist[v] + e.cost; que.push(e.to); } } return dist; } // Ο(V+E) // constraint: cost of each edge is zero or x (>= 0) std::vector zero_one_bfs(int s) const { std::vector dist(size(), INF); std::deque deq; dist[s] = 0; deq.push_back(s); while(!deq.empty()) { int v = deq.front(); deq.pop_front(); for(auto &e : edges[v]) { if(dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; e.cost ? deq.push_back(e.to) : deq.push_front(e.to); } } } return dist; } // Ο((E+V) lg E) // unreachable: INF std::vector dijkstra(int s) const { std::vector dist(size(), INF); const auto compare = [](const std::pair &a, const std::pair &b) { return a.first > b.first; }; std::priority_queue, std::vector>, decltype(compare)> que{compare}; dist[s] = 0; que.emplace(0, s); while(!que.empty()) { std::pair p = que.top(); que.pop(); int v = p.second; if(dist[v] < p.first) continue; for(auto &e : edges[v]) { if(dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; que.emplace(dist[e.to], e.to); } } } return dist; } // Ο(VE) // unreachable: INF // reachable via negative cycle: -INF std::vector bellman_ford(int s) const { int n = size(); std::vector res(n, INF); res[s] = 0; for(int loop = 0; loop < n - 1; loop++) { for(int v = 0; v < n; v++) { if(res[v] == INF) continue; for(auto &e : edges[v]) { res[e.to] = std::min(res[e.to], res[v] + e.cost); } } } std::queue que; std::vector chk(n); for(int v = 0; v < n; v++) { if(res[v] == INF) continue; for(auto &e : edges[v]) { if(res[e.to] > res[v] + e.cost and !chk[e.to]) { que.push(e.to); chk[e.to] = 1; } } } while(!que.empty()) { int now = que.front(); que.pop(); for(auto &e : edges[now]) { if(!chk[e.to]) { chk[e.to] = 1; que.push(e.to); } } } for(int i = 0; i < n; i++) if(chk[i]) res[i] = -INF; return res; } // Ο(V^3) std::vector> warshall_floyd() const { const int n = size(); std::vector> dist(n, std::vector(n, INF)); for(int i = 0; i < n; i++) dist[i][i] = 0; for(int i = 0; i < n; i++) for(auto &e : edges[i]) dist[i][e.to] = std::min(dist[i][e.to], e.cost); for(int k = 0; k < n; k++) for(int i = 0; i < n; i++) { if(dist[i][k] == INF) continue; for(int j = 0; j < n; j++) { if(dist[k][j] == INF) continue; dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]); } } return dist; } // Ο(V) (using DFS) // if a cycle exists, return {} std::vector topological_sort() const { std::vector res; std::vector used(size(), 0); bool not_DAG = false; auto dfs = [&](auto self, int k) -> void { if(not_DAG) return; if(used[k]) { if(used[k] == 1) not_DAG = true; return; } used[k] = 1; for(auto &e : edges[k]) self(self, e.to); used[k] = 2; res.push_back(k); }; for(int i = 0; i < size(); i++) dfs(dfs, i); if(not_DAG) return std::vector{}; std::reverse(res.begin(), res.end()); return res; } bool is_dag() const { return !topological_sort().empty(); } // Ο(V) // array of the distance to the most distant vertex // constraint: the graph is a tree std::vector height() const { auto vec1 = bfs(0); int v1 = -1, v2 = -1; cost_type dia = -1; for(int i = 0; i < int(size()); i++) if(dia < vec1[i]) dia = vec1[i], v1 = i; vec1 = bfs(v1); dia = -1; for(int i = 0; i < int(size()); i++) if(dia < vec1[i]) dia = vec1[i], v2 = i; auto vec2 = bfs(v2); for(int i = 0; i < int(size()); i++) { if(vec1[i] < vec2[i]) vec1[i] = vec2[i]; } return vec1; } // O(V+E) // vector<(int)(0 or 1)> // if it is not bipartite, return {} std::vector bipartite_grouping() const { std::vector colors(size(), -1); auto dfs = [&](auto self, int now, int col) -> bool { colors[now] = col; for(auto &e : edges[now]) { if(col == colors[e.to]) return false; if(colors[e.to] == -1 and !self(self, e.to, !col)) return false; } return true; }; for(int i = 0; i < int(size()); i++) if(colors[i] == -1 and !dfs(dfs, i, 0)) return std::vector{}; return colors; } bool is_bipartite() const { return !bipartite_grouping().empty(); } // Ο(V+E) // (v1, v2, diameter) std::tuple diameter() { std::vector dist = bfs(0); auto it = std::max_element(dist.begin(), dist.end()); const int v = it - dist.begin(); dist = bfs(v); it = std::max_element(dist.begin(), dist.end()); return std::make_tuple(v, int(it - dist.begin()), *it); } // Ο(V+E) std::vector subtree_size(const int root) { const int n = size(); std::vector ret(n, 1); auto dfs = [&](auto self, int now, int p = -1) -> void { for(const auto &e : (*this)[now]) { if(e.to == p) continue; self(self, e.to, now); ret[now] += ret[e.to]; } }; dfs(dfs, root); return ret; } // Ο(ElgE) cost_type prim() const { cost_type res = 0; std::priority_queue, std::greater> que; for(auto &e : edges[0]) que.push(e); std::vector chk(size()); chk[0] = 1; int cnt = 1; while(cnt < size()) { auto e = que.top(); que.pop(); if(chk[e.to]) continue; cnt++; res += e.cost; chk[e.to] = 1; for(auto &e2 : edges[e.to]) que.push(e2); } return res; } // Ο(ElgE) cost_type kruskal() const { std::vector> eds; for(const auto &adj : edges) for(const auto &ed : adj) eds.emplace_back(ed.from, ed.to, ed.cost); std::sort(eds.begin(), eds.end(), [](const std::tuple &a, const std::tuple &b) { return std::get<2>(a) < std::get<2>(b); }); std::vector uf_data(size(), -1); auto root = [&uf_data](auto self, int x) -> int { if(uf_data[x] < 0) return x; return uf_data[x] = self(self, uf_data[x]); }; auto unite = [&uf_data, &root](int u, int v) -> bool { u = root(root, u), v = root(root, v); if(u == v) return false; if(uf_data[u] > uf_data[v]) std::swap(u, v); uf_data[u] += uf_data[v]; uf_data[v] = u; return true; }; cost_type ret = 0; for(auto &e : eds) if(unite(std::get<0>(e), std::get<1>(e))) ret += std::get<2>(e); return ret; } // O(V) std::vector centroid() const { std::vector centroid, sz(size()); auto dfs = [&](auto self, int now, int per) -> void { sz[now] = 1; bool is_centroid = true; for(auto &e : edges[now]) { if(e.to != per) { self(self, e.to, now); sz[now] += sz[e.to]; if(sz[e.to] > size() / 2) is_centroid = false; } } if(size() - sz[now] > size() / 2) is_centroid = false; if(is_centroid) centroid.push_back(now); }; dfs(dfs, 0, -1); return centroid; } // O(V+E) // bridge: (s, t) (s < t); std::pair>, std::vector> bridges_and_articulations() const { std::vector order(size(), -1), low(size()), articulation; int order_next = 0; std::vector> bridge; auto dfs = [&](auto self, int now, int par = -1) -> void { low[now] = order[now] = order_next++; bool is_articulation = false; int cnt = 0; for(auto &ed : edges[now]) { int &nxt = ed.to; if(nxt == par) continue; if(order[nxt] == -1) { cnt++; self(self, nxt, now); if(order[now] < low[nxt]) bridge.push_back(std::minmax(now, nxt)); if(order[now] <= low[nxt]) is_articulation = true; low[now] = std::min(low[now], low[nxt]); } else if(order[now] > order[nxt]) { low[now] = std::min(low[now], order[nxt]); } } if(par == -1 and cnt < 2) is_articulation = false; if(is_articulation) articulation.push_back(now); return; }; for(int i = 0; i < (int)size(); i++) if(order[i] == -1) dfs(dfs, i); return std::make_pair(bridge, articulation); } // Ο(V+E) // directed graph from root to leaf graph root_to_leaf(int root = 0) const { graph res(size()); std::vector chk(size(), 0); chk[root] = 1; auto dfs = [&](auto self, int now) -> void { for(auto &e : edges[now]) { if(chk[e.to] == 1) continue; chk[e.to] = 1; res.add_edge(now, e.to, e.cost, 1, 0); self(self, e.to); } }; dfs(dfs, root); return res; } // Ο(V+E) // directed graph from leaf to root graph leaf_to_root(int root = 0) const { graph res(size()); std::vector chk(size(), 0); chk[root] = 1; auto dfs = [&](auto self, int now) -> void { for(auto &e : edges[now]) { if(chk[e.to] == 1) continue; chk[e.to] = 1; res.add_edge(e.to, now, e.cost, 1, 0); self(self, e.to); } }; dfs(dfs, root); return res; } // cost_type Chu_Liu_Edmonds(int root = 0) {} }; #pragma endregion #line 2 "/home/nok0/documents/programming/library/graph/tree_doubling.hpp" template struct tree_doubling { private: std::vector dist; std::vector depth; std::vector> parent; int max_jump = 1; void build() { for(int i = 0; i < max_jump - 1; i++) { for(int v = 0; v < (int)dist.size(); v++) { if(parent[i][v] == -1) parent[i + 1][v] = -1; else parent[i + 1][v] = parent[i][parent[i][v]]; } } } public: tree_doubling() = default; tree_doubling(const graph &g, const int root = 0) : dist(g.size()), depth(g.size()) { int n = g.size(); while((1 << max_jump) < n) max_jump++; parent.assign(max_jump, std::vector(n, -1)); auto dfs = [&](auto self, int now, int per, int d, cost_type cost) -> void { parent[0][now] = per; depth[now] = d; dist[now] = cost; for(auto &e : g[now]) if(e.to != per) self(self, e.to, now, d + 1, cost + e.cost); }; dfs(dfs, root, -1, 0, 0LL); build(); } int lowest_common_ancestor(int u, int v) { if(depth[u] < depth[v]) std::swap(u, v); int k = int(parent.size()); for(int i = 0; i < k; i++) if((depth[u] - depth[v]) >> i & 1) u = parent[i][u]; if(u == v) return u; for(int i = k - 1; i >= 0; i--) if(parent[i][u] != parent[i][v]) u = parent[i][u], v = parent[i][v]; return parent[0][u]; } cost_type length_of_path(const int u, const int v) { return dist[u] + dist[v] - dist[lowest_common_ancestor(u, v)] * 2; } int level_ancestor(int v, int level) { assert(level >= 0); for(int jump = 0; jump < max_jump and level; jump++) { if(level & 1) v = parent[jump][v]; level >>= 1; } return v; } }; #line 3 "/home/nok0/documents/programming/library/template/def_const.hpp" const int inf = 1000000000; const long long INF = 1000000000000000000ll; #line 4 "/home/nok0/documents/programming/library/template/debug.hpp" namespace viewer { void view(const long long &e) { if(e == INF) std::cerr << "INF"; else if(e == -INF) std::cerr << "-INF"; else std::cerr << e; } void view(const int &e) { if(e == inf) std::cerr << "inf"; else if(e == -inf) std::cerr << "-inf"; else std::cerr << e; } template void view(const T &e) { std::cerr << e; } template void view(const std::pair &p) { std::cerr << "("; view(p.first); std::cerr << ", "; view(p.second); std::cerr << ")"; } template void view(const std::tuple &p) { std::cerr << "("; view(std::get<0>(p)); std::cerr << ", "; view(std::get<1>(p)); std::cerr << ", "; view(std::get<2>(p)); std::cerr << ")"; } template void view(const std::tuple &p) { std::cerr << "("; view(std::get<0>(p)); std::cerr << ", "; view(std::get<1>(p)); std::cerr << ", "; view(std::get<2>(p)); std::cerr << ", "; view(std::get<3>(p)); std::cerr << ")"; } template void view(const std::set &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::unordered_set &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::multiset &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::unordered_multiset &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(const auto &e : v) { view(e); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::array &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(const auto &e : v) { view(e); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector> &vv) { std::cerr << "{\n"; for(const auto &v : vv) { std::cerr << "\t"; view(v); std::cerr << '\n'; } std::cerr << "}"; } template void view(const std::vector> &v) { std::cerr << "{\n"; for(const auto &c : v) { std::cerr << "\t("; view(c.first); std::cerr << ", "; view(c.second); std::cerr << ")\n"; } std::cerr << "}"; } template void view(const std::vector> &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << '{'; for(const auto &t : v) { std::cerr << "\n\t"; view(t); std::cerr << ","; } std::cerr << "\n}"; } template void view(const std::vector> &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << '{'; for(const auto &t : v) { std::cerr << "\n\t"; view(t); std::cerr << ","; } std::cerr << "\n}"; } template void view(const std::map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); std::cerr << "] : "; view(t.second); std::cerr << '\n'; } std::cerr << "}"; } template void view(const std::unordered_map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); std::cerr << "] : "; view(t.second); std::cerr << '\n'; } std::cerr << "}"; } } // namespace viewer // when compiling : g++ foo.cpp -DLOCAL #ifdef LOCAL void debug_out() {} template void debug_out(Head H, Tail... T) { viewer::view(H); std::cerr << ", "; debug_out(T...); } #define debug(...) \ do { \ std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \ debug_out(__VA_ARGS__); \ std::cerr << "\b\b]\n"; \ } while(0) #define dump(x) \ do { \ std::cerr << __LINE__ << " " << #x << " : "; \ viewer::view(x); \ std::cerr << '\n'; \ } while(0) #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #line 3 "/home/nok0/documents/programming/library/template/def_name.hpp" #define pb push_back #define eb emplace_back #define fi first #define se second #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define popcnt(x) __builtin_popcountll(x) template using V = std::vector; template using VV = std::vector>; template using pqup = std::priority_queue, std::greater>; using ll = long long; using ld = long double; using int128 = __int128_t; using pii = std::pair; using pll = std::pair; #line 3 "/home/nok0/documents/programming/library/template/fast_io.hpp" struct fast_io { fast_io() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(15); } } fast_io_; #line 3 "/home/nok0/documents/programming/library/template/input.hpp" template std::istream &operator>>(std::istream &is, std::pair &p) { is >> p.first >> p.second; return is; } template std::istream &operator>>(std::istream &is, std::vector &v) { for(T &i : v) is >> i; return is; } std::istream &operator>>(std::istream &is, __int128_t &a) { std::string s; is >> s; __int128_t ret = 0; for(int i = 0; i < (int)s.length(); i++) if('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0'; a = ret * (s[0] == '-' ? -1 : 1); return is; } namespace scanner { void scan(int &a) { std::cin >> a; } void scan(long long &a) { std::cin >> a; } void scan(std::string &a) { std::cin >> a; } void scan(char &a) { std::cin >> a; } void scan(char a[]) { std::ignore = std::scanf("%s", a); } void scan(double &a) { std::cin >> a; } void scan(long double &a) { std::cin >> a; } template void scan(std::pair &p) { std::cin >> p; } template void scan(std::vector &a) { std::cin >> a; } void INPUT() {} template void INPUT(Head &head, Tail &...tail) { scan(head); INPUT(tail...); } } // namespace scanner #define VEC(type, name, size) \ std::vector name(size); \ scanner::INPUT(name) #define VVEC(type, name, h, w) \ std::vector> name(h, std::vector(w)); \ scanner::INPUT(name) #define INT(...) \ int __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define STR(...) \ std::string __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #define LD(...) \ long double __VA_ARGS__; \ scanner::INPUT(__VA_ARGS__) #line 3 "/home/nok0/documents/programming/library/template/math.hpp" template inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; } template inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; } template T divup(T x, T y) { return (x + y - 1) / y; } template T POW(T a, long long n) { T ret = 1; while(n) { if(n & 1) ret *= a; a *= a; n >>= 1; } return ret; } long long POW(long long a, long long n, const int mod) { long long ret = 1; a = (a % mod + mod) % mod; while(n) { if(n & 1) (ret *= a) %= mod; (a *= a) %= mod; n >>= 1; } return ret; } template T bin_search(T ok, T ng, const F &f) { while(abs(ok - ng) > 1) { T mid = (ok + ng) >> 1; (f(mid) ? ok : ng) = mid; } return ok; } template T bin_search(T ok, T ng, const F &f, int loop) { for(int i = 0; i < loop; i++) { T mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } #line 3 "/home/nok0/documents/programming/library/template/output.hpp" template std::ostream &operator<<(std::ostream &os, const std::pair &p) { os << p.first << " " << p.second; return os; } template std::ostream &operator<<(std::ostream &os, const std::vector &a) { for(int i = 0; i < int(a.size()); ++i) { if(i) os << " "; os << a[i]; } return os; } std::ostream &operator<<(std::ostream &dest, __int128_t &value) { std::ostream::sentry s(dest); if(s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while(tmp != 0); if(value < 0) { --d; *d = '-'; } int len = std::end(buffer) - d; if(dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } template void print(const T a) { std::cout << a << '\n'; } template void print(Head H, Tail... T) { std::cout << H << ' '; print(T...); } template void println(const T a) { std::cout << a << '\n'; } template void println(const std::vector &a) { for(const auto &v : a) std::cout << v << '\n'; } template void println(Head H, Tail... T) { std::cout << H << '\n'; println(T...); } void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); } void No() { std::cout << "No\n"; } void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); } void NO() { std::cout << "NO\n"; } #line 2 "/home/nok0/documents/programming/library/template/rep.hpp" #define foa(v, a) for (auto &v : a) #define repname(a, b, c, d, e, ...) e #define rep(...) repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__) #define rep0(x) for (int rep_counter = 0; rep_counter < (x); ++rep_counter) #define rep1(i, x) for (int i = 0; i < (x); ++i) #define rep2(i, l, r) for (int i = (l); i < (r); ++i) #define rep3(i, l, r, c) for (int i = (l); i < (r); i += (c)) #define repsname(a, b, c, ...) c #define reps(...) repsname(__VA_ARGS__, reps1, reps0)(__VA_ARGS__) #define reps0(x) for (int reps_counter = 1; reps_counter <= (x); ++reps_counter) #define reps1(i, x) for (int i = 1; i <= (x); ++i) #define rrepname(a, b, c, ...) c #define rrep(...) rrepname(__VA_ARGS__, rrep1, rrep0)(__VA_ARGS__) #define rrep0(x) for (int rrep_counter = (x)-1; rrep_counter >= 0; --rrep_counter) #define rrep1(i, x) for (int i = (x)-1; i >= 0; --i) #line 3 "/home/nok0/documents/programming/library/template/string_converter.hpp" struct string_converter { char start = 0; char type(const char &c) const { return (islower(c) ? 'a' : isupper(c) ? 'A' : isdigit(c) ? '0' : 0); } int convert(const char &c) { if(!start) start = type(c); return c - start; } int convert(const char &c, const std::string &chars) { return chars.find(c); } template auto convert(const T &v) { std::vector ret; ret.reserve(size(v)); for(auto &&e : v) ret.emplace_back(convert(e)); return ret; } template auto convert(const T &v, const std::string &chars) { std::vector ret; ret.reserve(size(v)); for(auto &&e : v) ret.emplace_back(convert(e, chars)); return ret; } int operator()(const char &v, char s = 0) { start = s; return convert(v); } int operator()(const char &v, const std::string &chars) { return convert(v, chars); } template auto operator()(const T &v, char s = 0) { start = s; return convert(v); } template auto operator()(const T &v, const std::string &chars) { return convert(v, chars); } } toint; #line 3 "/home/nok0/documents/programming/library/template/vector.hpp" template int lb(const std::vector &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); } template int ub(const std::vector &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); } template void UNIQUE(std::vector &a) { std::sort(a.begin(), a.end()); a.erase(std::unique(a.begin(), a.end()), a.end()); } template std::vector press(std::vector &a) { auto res = a; UNIQUE(res); for(auto &v : a) v = lb(res, v); return res; } #define SORTname(a, b, c, ...) c #define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__) #define SORT0(a) std::sort((a).begin(), (a).end()) #define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; }) template void ADD(std::vector &a, const T x = 1) { for(auto &v : a) v += x; } template void SUB(std::vector &a, const T x = 1) { for(auto &v : a) v -= x; } template struct cum_vector { public: cum_vector() = default; template cum_vector(const std::vector &vec) : cum((int)vec.size() + 1) { for(int i = 0; i < (int)vec.size(); i++) cum[i + 1] = cum[i] + vec[i]; } T prod(int l, int r) { return cum[r] - cum[l]; } private: std::vector cum; }; std::vector> rle(const std::string &s) { const int n = s.size(); std::vector> ret; ret.reserve(n); for(int l = 0; l < n;) { int r = l + 1; for(; r < n and s[l] == s[r]; r++) {} ret.emplace_back(s[l], r - l); l = r; } return ret; } template std::vector> rle(const std::vector &v) { const int n = v.size(); std::vector> ret; ret.reserve(n); for(int l = 0; l < n;) { int r = l + 1; for(; r < n and v[l] == v[r]; r++) {} ret.emplace_back(v[l], r - l); l = r; } return ret; } std::vector iota(int n) { std::vector p(n); std::iota(p.begin(), p.end(), 0); return p; } #line 12 "/home/nok0/documents/programming/library/template/all" using namespace std; #line 6 "a.cpp" void main_(); int main() { int t = 1; while(t--) main_(); } using T = tuple; void main_() { LL(n, k, c); V es; rep(i, k) { INT(u, v, w, p); --u, --v; es.pb({u, v, w, p}); } auto f = [&](T x, T y) { return get<2>(x) < get<2>(y); }; sort(all(es), f); atcoder::dsu uf(n); graph g(n); ll wei = 0; ll ans = 0; V oth; for(auto [u, v, w, p] : es) { if(!uf.same(u, v)) { uf.merge(u, v); g.add_edge(u, v, w); wei += w; chmax(ans, p); } else { oth.push_back({u, v, w, p}); } } if(wei > c) { print(-1); return; } tree_doubling td(g); for(auto [u, v, w, p] : oth) { if(ans >= p) continue; ll len = td.length_of_path(u, v); debug(len); if(wei - len + w <= c) chmax(ans, p); } print(ans); // 利益を hoge 以上にできるか 判定 // 最小全域木を取る // }