QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#218813 | #6437. Paimon's Tree | xuyufeiya | WA | 329ms | 18404kb | C++20 | 3.6kb | 2023-10-18 18:51:21 | 2023-10-18 18:51:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 155, D = 9, M = 4;
vector<int> edges[N];
using ll = long long;
int a[N];
int sz[N][N], pre[N][N];
ll dp[N][N][N][M];
inline ll smx(ll &a, ll b) {
return a = max(a, b);
}
inline int tmx(int &a, int &b) {
if(a < b) swap(a, b);
return a;
}
inline ll tmx(ll &a, ll &b) {
if(a < b) swap(a, b);
return a;
}
void solve()
{
int n;
cin >> n;
n++;
for(int i = 1; i <= n; i++) {
if(i != n) cin >> a[i];
edges[i].clear();
}
int u, v;
for(int i = 1; i < n; i++) {
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
auto dfs = [&](auto self, int s, int u, int f) -> void {
sz[s][u] = 1;
for(int nxt: edges[u]) if(nxt != f) {
pre[s][nxt] = u;
self(self, s, nxt, u);
sz[s][u] += sz[s][nxt];
}
};
for(int i = 1; i <= n; i++) dfs(dfs, i, i, i);
// cout << "::0" << endl;
const ll INF = 1e15;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
for(int k = 0; k <= n; k++) {
for(int u = 0; u < M; u++) dp[i][j][k][u] = -INF;
}
}
}
// cout << "::1" << endl;
for(int i = 1; i <= n; i++) {
dp[i][i][1][0] = 0;
for(int j: edges[i]) {
if(i < j) dp[i][j][1][1] = 0;
else dp[j][i][1][1] = 0;
}
for(int j: edges[i]) {
for(int k: edges[i]) if(j != k) {
tmx(k, j);
dp[j][k][1][3] = 0;
}
}
}
// cout << "::1" << endl;
for(int t = 1; t < n; t++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) if(i != j) {
for(int m = 0; m < M; m++) {
if(m == 0) {
smx(dp[i][j][t+1][m], dp[i][j][t][m]);
} else if(m == 1) {
smx(dp[i][j][t+1][0], dp[i][j][t][m] + a[t]);
for(int nxt: edges[j]) if(nxt != pre[i][j]) smx(dp[i][nxt][t+1][m], dp[i][j][t][m] + a[t]);
int rem = n - 1 - sz[i][j] - t + 1;
if(rem) smx(dp[i][j][t+1][m], dp[i][j][t][m]);
} else if(m == 2) {
smx(dp[i][j][t+1][0], dp[i][j][t][m] + a[t]);
for(int nxt: edges[i]) if(nxt != pre[j][i]) smx(dp[nxt][j][t+1][m], dp[i][j][t][m] + a[t]);
int rem = n - 1 - sz[j][i] - t + 1;
if(rem) smx(dp[i][j][t+1][m], dp[i][j][t][m]);
} else {
smx(dp[i][j][t+1][1], dp[i][j][t][m] + a[t]);
smx(dp[i][j][t+1][2], dp[i][j][t][m] + a[t]);
for(int nxt: edges[j]) if(nxt != pre[i][j]) smx(dp[i][nxt][t+1][m], dp[i][j][t][m] + a[t]);
for(int nxt: edges[i]) if(nxt != pre[j][i]) smx(dp[nxt][j][t+1][m], dp[i][j][t][m] + a[t]);
int rem = n - 1 - sz[j][i] - sz[i][j] - t + 1;
if(rem) smx(dp[i][j][t+1][m], dp[i][j][t][m]);
}
}
}
}
}
ll ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
smx(ans, dp[i][j][n][0]);
}
}
printf("%lld\n", ans);
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7992kb
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: 329ms
memory: 18404kb
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 1875024569 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 86th numbers differ - expected: '2031440935', found: '1824255534'