QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#684963#6437. Paimon's Treecatch-up-from-behind#WA 317ms20132kbC++233.0kb2024-10-28 16:48:572024-10-28 16:48:57

Judging History

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

  • [2024-10-28 16:48:57]
  • 评测
  • 测评结果:WA
  • 用时:317ms
  • 内存:20132kb
  • [2024-10-28 16:48:57]
  • 提交

answer

// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\33[32m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T &x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T &x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
	x = 0; int f = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
	for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x *= f;
}
const int N = 160;
const ll INF = 1e18;
int n, a[N], ff[N][N], sz[N][N], dep[N][N];
ll dp[N][N][N][2][2];
vector <int> v[N];
void dfs(int rt, int x, int fa) {
	dep[rt][x] = dep[rt][fa] + 1;
	sz[rt][x] = 1;
	ff[rt][x] = fa;
	for (int i: v[x])
		if (i != fa) {
			dfs(rt, i, x);
			sz[rt][x] += sz[rt][i];
		}
}
void zhk() {
	ll ans = 0;
	read(n);
	F(i, 1, n + 1) v[i].clear();
	F(i, 1, n) read(a[i]), chkmax(ans, (ll) a[i]);
	F(i, 1, n) {
		int x, y; read(x), read(y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	F(i, 0, n)
		F(a, 1, n + 1)
			F(b, 1, n + 1)
				F(ta, 0, 1)
					F(tb, 0, 1)
						dp[i][a][b][ta][tb] = - INF;
	F(i, 1, n + 1) {
		dfs(i, i, 0);
	}
	F(i, 1, n)
		for (int j: v[i]) {
			dp[0][j][i][0][1] = 0;
			dp[0][j][i][1][0] = 0;
			dp[0][i][j][0][1] = 0;
			dp[0][i][j][1][0] = 0;
			for (int k: v[i])
				if (j != k) dp[0][j][k][0][0] = 0;
		}
	vector <pair <int, int>> g;
	F(i, 1, n + 1)
		F(j, 1, n + 1)
			g.emplace_back(i, j);
	sort(all(g), [&] (pair <int, int> x, pair <int, int> y) {
		return dep[x.first][x.second] < dep[y.first][y.second];
	});
	F(i, 0, n - 1)
		for (auto [a, b]: g) F(ta, 0, 1) F(tb, 0, 1) if (dp[i][a][b][ta][tb] != - INF) {
			// debug << a << " " << b << " " << dp[i][a][b] << endl;
			// chkmax(ans, dp[i][a][b] + ::a[i + 1]);
			if (ta == 0) {
				chkmax(dp[i + 1][a][b][1][tb], dp[i][a][b][ta][tb] + ::a[i + 1]);
				for (int t: v[a])
					if (t != ff[b][a]) {
						chkmax(dp[i + 1][t][b][ta][tb], dp[i][a][b][ta][tb] + ::a[i + 1]);
					}
			}
			if (tb == 0) {
				chkmax(dp[i + 1][a][b][ta][1], dp[i][a][b][ta][tb] + ::a[i + 1]);
				for (int t: v[b])
					if (t != ff[a][b]) {
						chkmax(dp[i + 1][a][t][ta][tb], dp[i][a][b][ta][tb] + ::a[i + 1]);
					}
			}
			int w = n - sz[a][b] - sz[b][a] + ta + tb;
			if (w > i) {
				chkmax(dp[i + 1][a][b][ta][tb], dp[i][a][b][ta][tb]);
			}
			chkmax(ans, dp[i][a][b][ta][tb]);
		}
	F(a, 1, n + 1)
		F(b, 1, n + 1) {
			chkmax(ans, dp[n][a][b][1][1]);
			F(ta, 0, 1)
				F(tb, 0, 1)
					if (!ta || !tb)
						assert(dp[n][a][b][ta][tb] == - INF);
			// debug << a << " " << b << " " << dp[n][a][b][1][1] << endl;
		}
	// debug << dp[1]
	cout << ans << '\n';
}
signed main() {
	int _ = 1;
	cin >> _;
	while (_--) zhk();
	return 0;
}
/* why?
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7740kb

input:

2
5
1 7 3 5 4
1 3
2 3
3 4
4 5
4 6
1
1000000000
1 2

output:

16
1000000000

result:

ok 2 number(s): "16 1000000000"

Test #2:

score: -100
Wrong Answer
time: 317ms
memory: 20132kb

input:

5000
19
481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783
9 4
4 18
12 9
10 9
4 1
6 12
2 18
9 13
6 14
4 8
2 3
10 17
2 20
19 20
5 1
12 15
15 16
4 7
17 11
4
240982681 ...

output:

5750811120
1896999359
4208559611
4140156713
5361004844
1386558912
3690026656
3702623113
3412485417
7807375141
5341435147
2355946110
3090496776
5626636202
4729664767
2207592767
572868833
4759005973
2944749369
2538044586
3083947956
5757497518
1421427135
3971435093
1197051728
396588615
251138097
221986...

result:

wrong answer 6th numbers differ - expected: '1875024569', found: '1386558912'