#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") struct Hld { int n, rt; // needs to be tree // vertex lists // modified in build(rt) (parent removed, heavy child first) vector> graph; vector sz, par, dep; int zeit; vector dis, fin, sid; // head vertex (minimum depth) in heavy path vector head; Hld() : n(0), rt(-1), zeit(0) {} explicit Hld(int n_) : n(n_), rt(-1), graph(n), zeit(0) {} void ae(int u, int v) { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); graph[u].push_back(v); graph[v].push_back(u); } void dfsSz(int u) { sz[u] = 1; for (const int v : graph[u]) { auto it = std::find(graph[v].begin(), graph[v].end(), u); if (it != graph[v].end()) graph[v].erase(it); par[v] = u; dep[v] = dep[u] + 1; dfsSz(v); sz[u] += sz[v]; } } void dfsHld(int u) { dis[u] = zeit++; const int deg = graph[u].size(); if (deg > 0) { int vm = graph[u][0]; int jm = 0; for (int j = 1; j < deg; ++j) { const int v = graph[u][j]; if (sz[vm] < sz[v]) { vm = v; jm = j; } } swap(graph[u][0], graph[u][jm]); head[vm] = head[u]; dfsHld(vm); for (int j = 1; j < deg; ++j) { const int v = graph[u][j]; head[v] = v; dfsHld(v); } } fin[u] = zeit; } void build(int rt_) { assert(0 <= rt_); assert(rt_ < n); rt = rt_; sz.assign(n, 0); par.assign(n, -1); dep.assign(n, -1); dep[rt] = 0; dfsSz(rt); zeit = 0; dis.assign(n, -1); fin.assign(n, -1); head.assign(n, -1); head[rt] = rt; dfsHld(rt); assert(zeit == n); sid.assign(n, -1); for (int u = 0; u < n; ++u) sid[dis[u]] = u; } friend ostream &operator<<(ostream &os, const Hld &hld) { const int maxDep = *max_element(hld.dep.begin(), hld.dep.end()); vector ss(2 * maxDep + 1); int pos = 0, maxPos = 0; for (int j = 0; j < hld.n; ++j) { const int u = hld.sid[j]; const int d = hld.dep[u]; if (hld.head[u] == u) { if (j != 0) { pos = maxPos + 1; ss[2 * d - 1].resize(pos, '-'); ss[2 * d - 1] += '+'; } } else { ss[2 * d - 1].resize(pos, ' '); ss[2 * d - 1] += '|'; } ss[2 * d].resize(pos, ' '); ss[2 * d] += std::to_string(u); if (maxPos < static_cast(ss[2 * d].size())) { maxPos = ss[2 * d].size(); } } for (int d = 0; d <= 2 * maxDep; ++d) os << ss[d] << '\n'; return os; } bool contains(int u, int v) const { return (dis[u] <= dis[v] && dis[v] < fin[u]); } int lca(int u, int v) const { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); for (; head[u] != head[v]; ) (dis[u] > dis[v]) ? (u = par[head[u]]) : (v = par[head[v]]); return (dis[u] > dis[v]) ? v : u; } int jumpUp(int u, int d) const { assert(0 <= u); assert(u < n); assert(d >= 0); if (dep[u] < d) return -1; const int tar = dep[u] - d; for (u = head[u]; ; u = head[par[u]]) { if (dep[u] <= tar) return sid[dis[u] + (tar - dep[u])]; } } int jump(int u, int v, int d) const { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); assert(d >= 0); const int l = lca(u, v); const int du = dep[u] - dep[l], dv = dep[v] - dep[l]; if (d <= du) { return jumpUp(u, d); } else if (d <= du + dv) { return jumpUp(v, du + dv - d); } else { return -1; } } // [u, v) or [u, v] template void doPathUp(int u, int v, bool inclusive, F f) const { assert(contains(v, u)); for (; head[u] != head[v]; u = par[head[u]]) f(dis[head[u]], dis[u] + 1); if (inclusive) { f(dis[v], dis[u] + 1); } else { if (v != u) f(dis[v] + 1, dis[u] + 1); } } // not path order, include lca(u, v) or not template void doPath(int u, int v, bool inclusive, F f) const { const int l = lca(u, v); doPathUp(u, l, false, f); doPathUp(v, l, inclusive, f); } // (vs, ps): compressed tree // vs: DFS order (sorted by dis) // vs[ps[x]]: the parent of vs[x] // ids[vs[x]] = x, not set for non-tree vertex vector ids; pair, vector> compress(vector us) { // O(n) first time ids.resize(n, -1); std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (dis[u] < dis[v]); }); us.erase(std::unique(us.begin(), us.end()), us.end()); int usLen = us.size(); assert(usLen >= 1); for (int x = 1; x < usLen; ++x) us.push_back(lca(us[x - 1], us[x])); std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (dis[u] < dis[v]); }); us.erase(std::unique(us.begin(), us.end()), us.end()); usLen = us.size(); for (int x = 0; x < usLen; ++x) ids[us[x]] = x; vector ps(usLen, -1); for (int x = 1; x < usLen; ++x) ps[x] = ids[lca(us[x - 1], us[x])]; return make_pair(us, ps); } }; //////////////////////////////////////////////////////////////////////////////// // T: monoid representing information of an interval. // T() should return the identity. // T(S s) should represent a single element of the array. // T::pull(const T &l, const T &r) should pull two intervals. template struct SegmentTreePoint { int logN, n; vector ts; SegmentTreePoint() : logN(0), n(0) {} explicit SegmentTreePoint(int n_) { for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {} ts.resize(n << 1); } template explicit SegmentTreePoint(const vector &ss) { const int n_ = ss.size(); for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {} ts.resize(n << 1); for (int i = 0; i < n_; ++i) at(i) = T(ss[i]); build(); } T &at(int i) { return ts[n + i]; } void build() { for (int u = n; --u; ) pull(u); } inline void pull(int u) { ts[u].pull(ts[u << 1], ts[u << 1 | 1]); } // Changes the value of point a to s. template void change(int a, const S &s) { assert(0 <= a); assert(a < n); ts[a += n] = T(s); for (; a >>= 1; ) pull(a); } // Applies T::f(args...) to point a. template void ch(int a, F f, Args &&... args) { assert(0 <= a); assert(a < n); (ts[a += n].*f)(args...); for (; a >>= 1; ) pull(a); } // Calculates the product for [a, b). T get(int a, int b) { assert(0 <= a); assert(a <= b); assert(b <= n); if (a == b) return T(); a += n; b += n; T prodL, prodR, t; for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) { if (aa & 1) { t.pull(prodL, ts[aa++]); prodL = t; } if (bb & 1) { t.pull(ts[--bb], prodR); prodR = t; } } t.pull(prodL, prodR); return t; } // Calculates T::f(args...) of a monoid type for [a, b). // op(-, -) should calculate the product. // e() should return the identity. template #if __cplusplus >= 201402L auto #else decltype((std::declval().*F())()) #endif get(int a, int b, Op op, E e, F f, Args &&... args) { assert(0 <= a); assert(a <= b); assert(b <= n); if (a == b) return e(); a += n; b += n; auto prodL = e(), prodR = e(); for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) { if (aa & 1) prodL = op(prodL, (ts[aa++].*f)(args...)); if (bb & 1) prodR = op((ts[--bb].*f)(args...), prodR); } return op(prodL, prodR); } // Find min b s.t. T::f(args...) returns true, // when called for the partition of [a, b) from left to right. // Returns n + 1 if there is no such b. template int findRight(int a, F f, Args &&... args) { assert(0 <= a); assert(a <= n); if ((T().*f)(args...)) return a; if (a == n) return n + 1; a += n; for (; ; a >>= 1) if (a & 1) { if ((ts[a].*f)(args...)) { for (; a < n; ) { if (!(ts[a <<= 1].*f)(args...)) ++a; } return a - n + 1; } ++a; if (!(a & (a - 1))) return n + 1; } } // Find max a s.t. T::f(args...) returns true, // when called for the partition of [a, b) from right to left. // Returns -1 if there is no such a. template int findLeft(int b, F f, Args &&... args) { assert(0 <= b); assert(b <= n); if ((T().*f)(args...)) return b; if (b == 0) return -1; b += n; for (; ; b >>= 1) if ((b & 1) || b == 2) { if ((ts[b - 1].*f)(args...)) { for (; b <= n; ) { if (!(ts[(b <<= 1) - 1].*f)(args...)) --b; } return b - n - 1; } --b; if (!(b & (b - 1))) return -1; } } }; //////////////////////////////////////////////////////////////////////////////// constexpr Int INF = 1001001001001001001LL; struct NodeMin { Int mn; NodeMin() : mn(+INF) {} NodeMin(Int val) : mn(val) {} void pull(const NodeMin &l, const NodeMin &r) { mn = min(l.mn, r.mn); } void ch(Int val) { mn = val; } void chmin(Int val) { if (mn > val) mn = val; } bool test(Int tar) const { return (mn <= tar); } }; struct NodeMax { Int mx; NodeMax() : mx(-INF) {} NodeMax(Int val) : mx(val) {} void pull(const NodeMax &l, const NodeMax &r) { mx = max(l.mx, r.mx); } void ch(Int val) { mx = val; } void chmax(Int val) { if (mx < val) mx = val; } bool test(Int tar) const { return (mx >= tar); } }; //////////////////////////////////////////////////////////////////////////////// vector uf; int root(int u) { return (uf[u] < 0) ? u : (uf[u] = root(uf[u])); } bool connect(int u, int v) { u = root(u); v = root(v); if (u == v) return false; if (uf[u] > uf[v]) swap(u, v); uf[u] += uf[v]; uf[v] = u; return true; } int N, M; Int C; vector A, B, P; vector W; int main() { for (; ~scanf("%d%d%lld", &N, &M, &C); ) { A.resize(M); B.resize(M); W.resize(M); P.resize(M); for (int i = 0; i < M; ++i) { scanf("%d%d%lld%d", &A[i], &B[i], &W[i], &P[i]); --A[i]; --B[i]; } vector is(M); for (int i = 0; i < M; ++i) is[i] = i; sort(is.begin(), is.end(), [&](int i0, int i1) -> bool { return (W[i0] < W[i1]); }); uf.assign(N, -1); Int mst = 0; vector on(M, 0); int ans = -1; for (const int i : is) { if (connect(A[i], B[i])) { mst += W[i]; on[i] = 1; chmax(ans, P[i]); } } if (-uf[root(0)] == N && mst <= C) { Hld hld(N); for (int i = 0; i < M; ++i) if (on[i]) { hld.ae(A[i], B[i]); } hld.build(0); vector ws(N, 0); for (int i = 0; i < M; ++i) if (on[i]) { ws[max(hld.dis[A[i]], hld.dis[B[i]])] = W[i]; } // cerr<<"mst = "<