QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#723359 | #6438. Crystalfly | IsacBieber# | WA | 2ms | 7340kb | C++23 | 1.8kb | 2024-11-07 21:56:18 | 2024-11-07 21:56:22 |
Judging History
answer
#include<bits/stdc++.h>
#define debug(x) cerr<<#x<<":"<<x<<"\n"
using namespace std;
using ll = long long;
using pii = pair <int,int>;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int n, t[N];
ll ans, a[N], f[N], df[N], sums[N];
vector <vector<int>> e(N);
void dfs(int u,int fa)
{
f[u] = a[u];df[u] = 0; sums[u] = 0;
int m1 = 0, m2 = 0;
ll sum = 0;
for(auto son:e[u])
{
if(son!=fa)
{
dfs(son,u);
if(a[son] >= a[m1]) {
m2 = m1;
m1 = son;
}
else if(a[son] > a[m2]) {
m2 = son;
}
sum += f[son] - a[son];
df[u] += f[son];
sums[u] += a[son];
}
}
ll res = 0;
for (auto son : e[u]) {
if(son != fa) {
if(t[son] == 3) {
if(son == m1) {
res = std::max(res, f[son] + a[m2] + df[son] - sums[son]);
}
else {
res = std::max(res, f[son] + a[m1] + df[son] - sums[son]);
}
res = std::max(res, f[son] + sum - (f[son] - a[son]));
}
else {
res = std::max(res, f[son] + sum - (f[son] - a[son]));
}
}
}
f[u] += res;
ans = max(ans,f[u]);
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
f[i] = 0;
e[i].clear();
}
for(int i=1;i<=n;i++) cin>>t[i];
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
e[u].emplace_back(v);
e[v].emplace_back(u);
}
ans = 0;
dfs(1,0);
cout<<ans<<'\n';
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7340kb
input:
2 5 1 10 100 1000 10000 1 2 1 1 1 1 2 1 3 2 4 2 5 5 1 10 100 1000 10000 1 3 1 1 1 1 2 1 3 2 4 2 5
output:
10101 10111
result:
ok 2 number(s): "10101 10111"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 7308kb
input:
10 6 8 1 1 5 8 9 2 1 2 2 2 2 1 2 2 3 2 4 1 5 2 6 6 6 4 4 1 3 6 2 1 3 3 3 3 1 2 1 3 3 4 4 5 5 6 6 10 5 1 8 5 1 1 3 1 2 2 2 1 2 2 3 2 4 2 5 3 6 10 6 8 8 9 6 9 5 6 6 4 2 1 3 3 2 2 2 2 3 1 1 2 1 3 3 4 4 5 5 6 4 7 2 8 7 9 9 10 7 10 9 1 5 7 5 4 1 1 1 2 1 3 2 1 2 1 3 3 4 3 5 5 6 1 7 5 7 1 1 4 2 3 1 3 2 2 1...
output:
25 39 25 100 31 14 16 24 19 22
result:
wrong answer 2nd numbers differ - expected: '24', found: '39'