QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#406229#126. Balanced StringsI_Love_Sonechka#RE 0ms0kbC++172.0kb2024-05-06 22:47:172024-05-06 22:47:17

Judging History

This is the latest submission verdict.

  • [2024-05-06 22:47:17]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 0kb
  • [2024-05-06 22:47:17]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

// c++ short types
#define vt vector
typedef long long ll;
typedef long double ld;

void whattime() { cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl; }
const int inf = 1e9;
const int mod = 998244353;
bool debug = false;

void solve() {
	int n, m; cin >> n >> m;
	vt<vt<pair<int, int>>> g(n);
	for(int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w;
		g[--u].push_back({--v, w});
		g[v].push_back({u, w});
	}
	vt<int> cost(n);
	for(auto &x: cost) {
		cin >> x;
	}
	vt<ll> values;
	for(int i = 0; i < n; ++i) {
		vt<pair<ll, int>> dp(n);
//		cout << "i=" << i << "\n";
		vt<int> parent(n);
		auto Dfs = [&](auto self, int e, int p) -> void {
			parent[e] = p;
			dp[e] = {0, e};
			for(auto x: g[e]) if(x.first ^ p) {
				int to = x.first, w = x.second;
				self(self, to, e);
				dp[e] = max(dp[e], make_pair(dp[to].first + w, dp[to].second));
			}
//			cout << "e=" << e << " " << dp[e].first << " " << dp[e].second << "\n";
		};
		Dfs(Dfs, i, i);
		priority_queue<pair<ll, int>> q;
		vt<int> used(n, false);
		auto Add = [&](int id, int extra) {
			if(used[id]) {
				return ;
			}
			q.emplace(dp[id].first + extra, dp[id].second);
		};
		Add(i, 0);
		while(not q.empty()) {
			ll w = q.top().first;
			int u = q.top().second;
			q.pop();
//			cout << "u=" << u << " w=" << w << "\n";
			while(not used[u]) {
				used[u] = 1;
				for(auto to: g[u]) if(to.first ^ parent[u]) {
					Add(to.first, to.second);
				}
//				cout << u << " ";
				u = parent[u];
			}
//			cout << "@\n";
			values.push_back(w * cost[i]);
		}
//		cout << "\n";
	}
	sort(values.rbegin(), values.rend());
	ll res = 0;
	for(int i = 0; i < min(int(values.size()), m); ++i) {
		res += values[i];
	}
	cout << res << "\n";
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int tt = 1;
	if(debug) {
		tt = 1e3;
	} else {
//		cin >> tt;
	}
	for(int t = 0; t < tt; ++t) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

37589
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))...

output:


result: