QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#225520#6438. CrystalflyFYBGCWA 1ms7788kbC++171.9kb2023-10-24 19:21:522023-10-24 19:21:52

Judging History

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

  • [2023-10-24 19:21:52]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7788kb
  • [2023-10-24 19:21:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int N = 1e5 + 10;

int head[N], e[2 * N], ne[2 * N], t[N];
ll dp[N][2], w[N];
int idx, n;

inline void add(int a, int b)
{
    e[idx] = b, ne[idx] = head[a], head[a] = idx++;
}

inline void dfs(int u, int fa)
{
    int flag = 1;
    int M = 0;
    ll sum = 0;
    for (int i = head[u]; i; i = ne[i])
    {
        int v = e[i];
        if (v != fa)
        {
            flag = 0;
            dfs(v, u);

            if (w[v] > w[M])
                M = v;

            sum += dp[v][1];
        }
    }

    if (flag)
    {
        dp[u][1] = 0;
        dp[u][0] = 0;
        return;
    }

    dp[u][0] = sum;
    dp[u][1] = sum + w[M];

    ll x = 0, xx = 0;
    int vx, vxx;

    for (int i = head[u]; i; i = ne[i])
    {
        int v = e[i];
        if (v != fa)
        {
            ll y = w[v] - dp[v][1] + dp[v][0];
            if (y > x)
            {
                xx = x, vxx = vx;
                x = y, vx = v;
            }
            else if (y > xx)
            {
                xx = y, vxx = v;
            }
        }
    }

    for (int i = head[u]; i; i = ne[i])
    {
        int v = e[i];
        if (v != fa && t[v] == 3)
        {
            dp[u][1] = max(dp[u][1], sum + x + w[v]);
        }
    }
}

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i];
        head[i] = 0;
        dp[i][0] = dp[i][1] = 0;
    }
    for (int i = 1; i <= n; i++)
        cin >> t[i];

    int a, b;
    for (int i = 1; i < n; i++)
    {
        cin >> a >> b;
        add(a, b), add(b, a);
    }

    dfs(1, 0);
    cout << dp[1][1] + w[1] << '\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
    {
        idx = 1;
        solve();
    }
    return 0;
}

詳細信息

Test #1:

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

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: 7788kb

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
30
24
61
36
14
17
28
19
19

result:

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