QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#256274 | #6438. Crystalfly | Satsuki# | WA | 2ms | 12088kb | C++17 | 2.2kb | 2023-11-18 18:17:47 | 2023-11-18 18:17:47 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; ++ i)
#define int long long
using namespace std;
const int MAXN = 1e5 + 10;
int read()
{
char cc(getchar()); int cn(0), flus(1);
while(!isdigit(cc)) {if(cc == '-') flus = -flus; cc = getchar();}
while(isdigit(cc)) cn = cn * 10 + cc - 48, cc = getchar();
return cn * flus;
}
struct edge
{
int to, next;
} e[MAXN << 1];
struct node3
{
int val, id;
} mmax[MAXN][2];
int n, tot;
int spe[MAXN];
int head[MAXN], a[MAXN], t[MAXN];
int f[MAXN][5];
int g[MAXN];
void add(int u, int v)
{
e[++tot].next = head[u];
head[u] = tot;
e[tot].to = v;
}
void init()
{
tot = 0;
rep(i, 1, n) head[i] = 0;
rep(i, 1, n) rep(j, 0, 4) f[i][j] = 0;
rep(i, 1, n) g[i] = 0;
rep(i, 1, n) spe[i] = 0;
rep(i, 1, n) mmax[i][0].val = mmax[i][1].val = 0;
}
void dfs1(int u, int fa)
{
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if(v == fa) continue;
if(t[v] == 3) spe[u] = 1;
dfs1(v, u);
}
if(head[u] == 0)
{
f[u][0] = 0;
f[u][1] = 0;
}
}
void dfs2(int u, int fa)
{
int sumv(0);
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if(v == fa) continue;
dfs2(v, u);
if(t[v] == 3 && a[v] > mmax[u][0].val)
{
mmax[u][0].val = a[v];
mmax[u][0].id = v;
if(mmax[u][0].val > mmax[u][1].val)
{
swap(mmax[u][0], mmax[u][1]);
}
}
sumv += f[v][1];
}
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if(v == fa) continue;
f[u][0] = max(f[u][0], sumv);
f[u][1] = max(f[u][1], sumv + a[v]);
if(spe[u])
{
int tmp = sumv - f[v][1] + f[v][0] + a[v];
if(v == mmax[u][1].id) tmp += a[mmax[u][0].id];
else tmp += a[mmax[u][1].id];
f[u][1] = max(f[u][1], tmp);
}
}
}
signed main()
{
int T = read();
while(T --)
{
n = read();
init();
rep(i, 1, n) a[i] = read();
rep(i, 1, n) t[i] = read();
rep(i, 1, n - 1)
{
int u = read(), v = read();
add(u, v);
add(v, u);
}
dfs1(1, 0);
dfs2(1, 0);
int ans(0);
ans = f[1][1] + a[1];
printf("%lld\n", ans);
}
return 0;
}
/*
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
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12012kb
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: 12088kb
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 59 36 14 16 28 19 19
result:
wrong answer 4th numbers differ - expected: '56', found: '59'