結果

問題 No.2642 Don't cut line!
ユーザー kwm_tkwm_t
提出日時 2024-02-25 16:25:35
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 4,000 ms
コード長 3,979 bytes
コンパイル時間 5,646 ms
コンパイル使用メモリ 325,140 KB
実行使用メモリ 51,780 KB
最終ジャッジ日時 2024-02-25 16:25:47
合計ジャッジ時間 11,889 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 185 ms
51,164 KB
testcase_02 AC 153 ms
51,780 KB
testcase_03 AC 151 ms
48,044 KB
testcase_04 AC 147 ms
47,800 KB
testcase_05 AC 153 ms
49,400 KB
testcase_06 AC 41 ms
6,676 KB
testcase_07 AC 41 ms
6,676 KB
testcase_08 AC 41 ms
6,676 KB
testcase_09 AC 41 ms
6,676 KB
testcase_10 AC 42 ms
6,676 KB
testcase_11 AC 41 ms
6,676 KB
testcase_12 AC 43 ms
6,676 KB
testcase_13 AC 41 ms
6,676 KB
testcase_14 AC 41 ms
6,676 KB
testcase_15 AC 42 ms
6,676 KB
testcase_16 AC 76 ms
17,788 KB
testcase_17 AC 131 ms
43,568 KB
testcase_18 AC 147 ms
44,476 KB
testcase_19 AC 100 ms
35,832 KB
testcase_20 AC 55 ms
16,256 KB
testcase_21 AC 43 ms
6,676 KB
testcase_22 AC 56 ms
14,104 KB
testcase_23 AC 163 ms
48,628 KB
testcase_24 AC 69 ms
21,632 KB
testcase_25 AC 63 ms
19,584 KB
testcase_26 AC 51 ms
9,984 KB
testcase_27 AC 96 ms
33,112 KB
testcase_28 AC 168 ms
45,152 KB
testcase_29 AC 54 ms
12,800 KB
testcase_30 AC 60 ms
17,664 KB
testcase_31 AC 115 ms
38,848 KB
testcase_32 AC 57 ms
15,488 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint998244353;
//const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
#include <vector>
using  S = long long;
S op(S a, S b) { return max(a, b); }
S e() { return 0; }
struct HeavyLightDecomposition {
	HeavyLightDecomposition(std::vector<std::vector<int>> &_g, std::vector<S>&_val) :g(_g), val(_val) {
		n = g.size();
		sz.resize(n);
		par.resize(n);
		in.resize(n);
		out.resize(n);
		rev.resize(n);
		head.resize(n);
		deep.resize(n);
	}
	int n;
	std::vector<std::vector<int>>g;
	std::vector<int>sz, par, in, out, rev, head, deep;
	std::vector<S>val;
	segtree<S, op, e>seg;
	void build() {
		dfs_sz(0);
		int time = 0;
		dfs_hld(0, time);
		initSegmentTree();
	}
	void dfs_sz(int v, int d = 0, int p = -1) {
		deep[v] = d;
		par[v] = p;
		sz[v] = 1;
		if (g[v].size() && (p == g[v][0]))std::swap(g[v][0], g[v].back());
		for (auto &e : g[v]) {
			if (p == e)continue;
			dfs_sz(e, d + 1, v);
			sz[v] += sz[e];
			if (sz[g[v][0]] < sz[e])std::swap(g[v][0], e);
		}
	}
	void dfs_hld(int v, int &time, int p = -1) {
		in[v] = time;
		time++;
		rev[in[v]] = v;
		for (auto &e : g[v]) {
			if (p == e)continue;
			if (e == g[v][0])head[e] = head[v];
			else head[e] = e;
			dfs_hld(e, time, v);
		}
		out[v] = time;
	}
	void initSegmentTree() {
		std::vector<S>_val(n);
		for (int i = 0; i < n; ++i)_val[i] = val[rev[i]];
		seg = segtree<S, op, e>(_val);
	}
	S query(int u, int v) {
		S ret = e();
		for (;; v = par[head[v]]) {
			if (in[u] > in[v])std::swap(u, v);
			if (head[u] == head[v])break;
			auto get = seg.prod(in[head[v]], in[v] + 1);
			ret = op(ret, get);
		}
		auto get = seg.prod(in[u], in[v] + 1);
		ret = op(ret, get);
		return ret;
	}
	int la(int v, int k) {
		while (1) {
			int u = head[v];
			if (in[v] - k >= in[u])return rev[in[v] - k];
			k -= in[v] - in[u] + 1;
			v = par[u];
		}
	}
	int lca(int u, int v) {
		for (;; v = par[head[v]]) {
			if (in[u] > in[v])std::swap(u, v);
			if (head[u] == head[v])return u;
		}
	}
	int dist(int u, int v) {
		return deep[u] + deep[v] - 2 * deep[lca(u, v)];
	}
	// point update
	void update_point(int u, S x) {
		auto get = seg.get(in[u]);
		seg.set(in[u], get + 1);
	}
	void debug() {
		for (int i = 0; i < n; ++i) std::cout << seg.prod(i, i + 1) << " ";
		std::cout << std::endl;
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m; cin >> n >> m;
	long long c; cin >> c;
	struct Edge {
		int u, v, w, p;
		bool operator<(const Edge& other)const {
			return w < other.w;
		}
	};
	vector<Edge>e(m);
	rep(i, m) {
		cin >> e[i].u >> e[i].v >> e[i].w >> e[i].p;
		e[i].u--;
		e[i].v--;
	}
	sort(all(e));
	vector<vector<int>>to(2 * n - 1);
	dsu uf(n);
	int ans = 0;
	long long mncost = 0;
	vector<S>v(n * 2 - 1);
	int idx = n;
	for (auto &e : e) {
		if (uf.same(e.u, e.v))continue;
		to[e.u].emplace_back(idx);
		to[idx].emplace_back(e.u);
		to[idx].emplace_back(e.v);
		to[e.v].emplace_back(idx);
		chmax(ans, e.p);
		mncost += e.w;
		uf.merge(e.u, e.v);
		v[idx] = e.w;
		idx++;
	}
	if (mncost > c) {
		cout << -1 << endl;
		return 0;
	}
	HeavyLightDecomposition hld(to, v);
	hld.build();
	for (auto &e : e) {
		if (e.p <= ans) continue;
		auto get = hld.query(e.u, e.v);
		if (mncost - get + e.w <= c)ans = e.p;
	}
	cout << ans << endl;
	return 0;
}
0