QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#643649 | #6438. Crystalfly | longyin | WA | 2ms | 11828kb | C++20 | 2.7kb | 2024-10-15 22:37:28 | 2024-10-15 22:37:29 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e6 + 10;
vector<int> edges[N];
int dp[N][2], f[N][2], g[N],Max[N];
bool state[N];
int val[N];
int pos[N];
#define x first
#define y second
void dfs(int u, int fa) {
//dp[u][1] = val[u];
for (int v : edges[u]) {
if (v == fa)
continue;
dfs(v, u);
//g[u] = max(g[u], );
}
//vector<pair<int,int> > x,y;
//int ans1=0; int ans2=0;
int ans=0;
pair<int,int> a,b;
int x=0; int y=0;
for (int v : edges[u]) {
if (v == fa)
continue;
//子树选1,1;
Max[u]=max(Max[u],dp[v][0]);
// x.push_back({dp[v][0],v});
// y.push_back({dp[v][1],v});
x+=dp[v][0];
y=max(y,val[v]);
//if(u==1) cout<<v<<endl;
if(1)
{
int temp=Max[v]+val[v];
if(temp>a.x)
{
b=a;
a={temp,v};
} else if(temp>=b.x) b={temp,v};
}
//if(u==1) cout<<ans<<" "<<dp[u][0]<<endl;
}
for(int v:edges[u])
{
if(v==fa) continue;
if(state[v])
{
if(a.y==v)
{
dp[u][0]=max(dp[u][0],b.x+dp[v][1]);
}else dp[u][0]=max(dp[u][0],a.x+dp[v][1]);
}
}
dp[u][0]=max(dp[u][0],x+y);
dp[u][1]=dp[u][0]+val[u];
return ;
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
edges[i].clear();
dp[i][0] = dp[i][1] = 0;
f[i][0] = f[i][1] = 0;
}
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
state[i] = (t == 3);
}
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
edges[u].emplace_back(v);
edges[v].emplace_back(u);
}
// for (int i = 1; i <= n; i++) {
// sort(edges[i].begin(), edges[i].end(), [&](const auto& p, const auto& q) {
// return val[p] > val[q];
// });
// pos[i] = edges[i].size() > 0 ? edges[i][0] : 0;
// f[i][0] = edges[i].size() > 0 ? val[edges[i][0]] : 0;
// f[i][1] = edges[i].size() > 1 ? val[edges[i][1]] : 0;
// }
dfs(1, 0);
// for(int i=1;i<=n;i++)
// {
// cout<<Max[i]<<' ';
// cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
// }
cout << dp[1][1]<< endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
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: 11740kb
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: 11828kb
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 57 31 14 41 44 19 36
result:
wrong answer 4th numbers differ - expected: '56', found: '57'