QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#228098#7588. Monster HunterGiga_Cronos#WA 263ms45012kbC++231.9kb2023-10-28 11:59:132023-10-28 11:59:14

Judging History

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

  • [2023-10-28 11:59:14]
  • 评测
  • 测评结果:WA
  • 用时:263ms
  • 内存:45012kb
  • [2023-10-28 11:59:13]
  • 提交

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];
ll ending_min[MAXN];

void dfs(int u, int p) {
	vector<pip> sons;
	ending_min[u] = 1e18;
	ll summm = b[u] - a[u];
	for (auto v : g[u]) {
		if (v == p)
			continue;
		dfs(v, u);
		ending_min[u] = min(ending_min[u], ending_min[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";
		ending_min[u] = b[u];
		// cout << ending_min[u] << "\n";
		return;
	}
	sort(all(sons));
	ll act = b[u];
	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 (ending_min[u] + res > act) {
		dp[u] += ending_min[u] + res - act;
		act = ending_min[u] + res;
	}
	best[u] = max(best[u], act);
	fin[u] = dp[u] + summm;
	// cout << u << ' ' << dp[u] << ' ' << best[u] << ' ' << fin[u] << "\n";
	// cout << ending_min[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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 263ms
memory: 45012kb

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
541156749
191617241
177090559
383609891
27231993394
19064806867
37194996614
233991081
84657366

result:

wrong answer 9th numbers differ - expected: '84584890', found: '233991081'