QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181010 | #6438. Crystalfly | 0x4F5DA2 | RE | 0ms | 0kb | C++14 | 1.9kb | 2023-09-16 15:04:40 | 2023-09-16 15:04:40 |
answer
#include <cstdio>
const int maxn = 100000;
int n;
int a[maxn + 10];
int t[maxn + 10];
struct node{
int nxt;
int to;
}edge[maxn * 2 + 10];
int head[maxn + 10];
int top;
void add_e(int u, int v){
++top;
edge[top].nxt = head[u];
edge[top].to = v;
head[u] = top;
return ;
}
long long dp[maxn + 10][2];
void dfs(int x, int fa){
dp[x][0] = dp[x][1] = 0;
int ap1 = 0, ap2 = 0, ap3;
int ay1 = 0, ay2 = 0;
for(int i = head[x]; i; i = edge[i].nxt){
int y = edge[i].to;
if(y != fa){
dfs(y, x);
if(a[ap1] < a[y]){
ap1 = y;
}
if(t[y] == 3){
if(a[ap2] <= a[y]){
ap3 = ap2;
ap2 = y;
}
else if(a[ap3] < a[y]){
ap3 = y;
}
}
dp[x][1] = dp[x][1] + dp[y][0];
if(-dp[y][0] + dp[y][1] + a[y] >= -dp[ay1][0] + dp[ay1][1] + a[ay1]){
ay2 = ay1;
ay1 = y;
}
else if(-dp[y][0] + dp[y][1] + a[y] > -dp[ay2][0] + dp[ay2][1] + a[ay2]){
ay2 = y;
}
}
}
dp[x][0] = dp[x][1] + a[ap1];
if(ap2 != ay1){
long long temp = dp[x][1] + a[ap2] - dp[ay1][0] + dp[ay1][1] + a[ay1];
if(dp[x][0] < temp){
dp[x][0] = temp;
}
}
else{
long long temp = dp[x][1] + a[ap3] - dp[ay1][0] + dp[ay1][1] + a[ay1];
if(dp[x][0] < temp){
dp[x][0] = temp;
}
temp = dp[x][1] + a[ap2] - dp[ay2][0] + dp[ay2][1] + a[ay2];
if(dp[x][0] < temp){
dp[x][0] = temp;
}
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; ++i){
scanf("%d", &t[i]);
}
int u, v;
for(int i = 1; i < n; ++i){
scanf("%d%d", &u, &v);
add_e(u, v);
add_e(v, u);
}
dfs(1, 0);
printf("%lld\n", dp[1][0] + a[1]);
top = 0;
for(int i = 1; i <= n; ++i){
head[i] = 0;
}
}
return 0;
}
/*
1
10
6 5 3 7 1 5 1 9 7 3
3 1 3 3 1 3 2 2 2 3
1 2
1 3
3 4
4 5
2 6
6 7
4 8
7 9
1 10
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
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