QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#638386#6438. CrystalflywxiaobaivvWA 7ms31652kbC++141.6kb2024-10-13 15:48:062024-10-13 15:48:08

Judging History

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

  • [2024-10-13 15:48:08]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:31652kb
  • [2024-10-13 15:48:06]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#define int long long 
using namespace std;
const int N=1e6+10;
vector<int> rs[N];
int dp[N][2];//0
int n,v[N],tm[N];

int get(int c)
{
    return -(dp[c][1]+v[c]-dp[c][0]);
}

void dfs(int u,int last)
{
    int l1=-1,l2=-2;
    int m1=-1,m2=-2;
    int am=-1;
    for(auto c:rs[u])
    {
        if(c==last) continue;
        dfs(c,u);
        dp[u][1]+=dp[c][0];

        if(am==-1||v[c]>v[am]) am=v[c];

        if(tm[c]==3)
        {
            if(m1==-1||v[c]>v[m1]) m2=m1,m1=c;
            else if(m2==-1||v[c]>v[m2]) m2=c;
        }
        if(l1==-1||get(c)<get(l1)) l2=l1,l1=c;
        else if(l2==-1||get(c)<get(l2)) l2=c;
    }
    if(am!=-1)
    dp[u][0]=dp[u][1]+v[am];
   
    if(m1!=-1)
        if(l1!=m1&&l1!=-1) dp[u][0]=max(dp[u][0],dp[u][1]+v[m1]-get(l1));
        else if(l2!=m1&&l2!=-1) dp[u][0]=max(dp[u][0],dp[u][1]+v[m1]-get(l2));
    
    if(m2!=-1)
        if(l1!=m2&&l1!=-1) dp[u][0]=max(dp[u][0],dp[u][1]+v[m2]-get(l1));
        else if(l2!=m2&&l2!=-1) dp[u][0]=max(dp[u][0],dp[u][1]+v[m2]-get(l2));
    // cout<<u<<" "<<dp[u][0]<<"|"<<dp[u][1]<<"\n";
}

void slove()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i];
        rs[i].clear();
        dp[i][0]=dp[i][1]=0;
    }
    for(int i=1;i<=n;i++) cin>>tm[i];
    for(int i=1;i<n;i++)
    {
        int l,r; cin>>l>>r;
        rs[l].push_back(r);
        rs[r].push_back(l);
    }
    dfs(1,1);
    cout<<dp[1][0]+v[1]<<"\n";
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int ts=1; cin>>ts;
    while(ts--) slove();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 31652kb

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: 0ms
memory: 31456kb

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
35
64
33
20
17
31
25
19

result:

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