QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#228083#7588. Monster HunterGiga_Cronos#WA 322ms43568kbC++231.7kb2023-10-28 11:39:142023-10-28 11:39:14

Judging History

你现在查看的是最新测评结果

  • [2023-10-28 11:39:14]
  • 评测
  • 测评结果:WA
  • 用时:322ms
  • 内存:43568kb
  • [2023-10-28 11:39:14]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<ll, pii> pip;
#define MAXN (ll)(4e5 + 5)
const ll mod = (ll)(1e9 + 7);

ll a[MAXN], b[MAXN];
vector<int> g[MAXN];
ll dp[MAXN], best[MAXN], fin[MAXN];

void dfs(int u, int p) {
	vector<pip> sons;
	ll minimal_fin_v = 1e18;
	ll summm = b[u] - a[u];
	for (auto v : g[u]) {
		if (v == p)
			continue;
		dfs(v, u);
		minimal_fin_v = min(minimal_fin_v, fin[v]);
		sons.push_back(pip(dp[v], pii(best[v] - dp[v], best[v] - fin[v])));
		summm += (fin[v] - dp[v]);
	}
	dp[u] = a[u];
	best[u] = max(a[u], b[u]);
	fin[u] = b[u];
	if (sons.empty()) {
		// cout << u << ' ' << dp[u] << ' ' << best[u] << ' ' << fin[u] << "\n";
		return;
	}
	ll act = b[u];
	sort(all(sons));
	ll res = 0;
	for (auto p : sons) {
		ll mi = p.first;
		ll add = p.second.first;
		ll men = p.second.second;
		res += men;
		if (mi > act) {
			dp[u] += mi - act;
			act = mi;
		}
		act += add;
	}
	if (minimal_fin_v + res > act) {
		dp[u] += minimal_fin_v + res - act;
		act = minimal_fin_v + res;
	}
	best[u] = max(best[u], act);
	fin[u] = dp[u] + summm;
	// cout << u << ' ' << dp[u] << ' ' << best[u] << ' ' << fin[u] << "\n";
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		for (int i = 1; i < n; i++)
			cin >> a[i] >> b[i];
		for (int i = 0; i < n - 1; i++) {
			int u, v;
			cin >> u >> v;
			u--, v--;
			g[u].push_back(v);
			g[v].push_back(u);
		}

		dfs(0, -1);

		cout << dp[0] << "\n";

		for (int i = 0; i < n; i++) {
			a[i] = b[i] = 0;
			dp[i] = best[i] = fin[i] = 0;
			g[i].clear();
		}
	}

	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 19904kb

input:

1
4
2 6
5 4
6 2
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: -100
Wrong Answer
time: 322ms
memory: 43568kb

input:

10
50000
112474 198604
847262 632597
871374 962220
971398 284540
10360 695245
43233 569941
64019 203468
598469 997911
41841 657552
333198 936119
546370 180011
58831 901040
959767 396595
710441 277461
429299 209831
494164 138327
393982 581911
993164 617488
108113 160310
55841 611360
301395 291074
149...

output:

63495498
541157617
191617241
177090559
383609891
27232342028
19065024046
37196811824
233991081
84657366

result:

wrong answer 2nd numbers differ - expected: '541156749', found: '541157617'