QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#723381#6438. CrystalflyIsacBieber#WA 2ms7424kbC++231.9kb2024-11-07 22:01:172024-11-07 22:01:17

Judging History

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

  • [2024-11-07 22:01:17]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7424kb
  • [2024-11-07 22:01:17]
  • 提交

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] + sum - (f[m2] - a[m2]) - (f[son] - a[son]));
                }
                else {
                    res = std::max(res, f[son] + a[m1] + df[son] - sums[son] + sum - (f[m1] - a[m1]) - (f[son] - a[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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 7320kb

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
28
19
22

result:

wrong answer 2nd numbers differ - expected: '24', found: '39'