QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#662832 | #6438. Crystalfly | 122 | WA | 2ms | 8304kb | C++14 | 1.5kb | 2024-10-21 10:58:29 | 2024-10-21 10:58:29 |
Judging History
answer
#include <bits/stdc++.h>
#define LL long long
#define pair<int,int> PII
using namespace std;
const int N=1e5+10;
int f[N][2];//取不取叶子节点的节点u
int a[N],tot[N];
vector<int>g[N];
vector<int>G[N];
void dfs(int u,int fa)
{
int maxx=0,res=0;f[u][0] = f[u][1] = 0;
for(int v:G[u])
{
if(v==fa) continue;
dfs(v,u);
res+=f[v][1];
maxx=max(a[v],maxx);
}
f[u][0]=res; f[u][1]=res+maxx;//取子节点和不取子节点的相对状态
int max1=-2e18,t1=0,max2=-2e18,t2=0;
if(g[u].size())
{
for(int v:g[u])//可以转移的相邻节点
{
if(v==fa) continue;
if(a[v]>max1){max2=max1;t2=t1;max1=a[v];t1=v;}
else if(a[v]>max2){max2=a[v];t2=v;}
}int maxv=0;
for(int v:G[u])
{
if(v==fa) continue;
if(v==max1)
{
if(t2==0) maxv=max(maxv,f[v][0]+res-f[v][1]+a[v]);
else maxv=max(maxv,f[v][0]-f[v][1]+a[v]+max2+res);
}
else maxv=max(maxv,f[v][0]-f[v][1]+a[v]+max1+res);
}
f[u][1]=max(f[u][1],maxv);
}
}
void solve()
{
int n;
cin>>n;for(int i=1;i<=n;i++) g[i].clear(),G[i].clear();
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>tot[i];
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;G[a].push_back(b);G[b].push_back(a);
if(tot[a]==3)g[b].push_back(a);
if(tot[b]==3)g[a].push_back(b);
}
dfs(1,0);
cout<<f[1][1]+a[1]<<endl;
}
int main() {
// ios_base::sync_with_stdio(false);
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: 8228kb
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: 8304kb
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 24 24 61 36 14 17 28 19 19
result:
wrong answer 4th numbers differ - expected: '56', found: '61'