QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#357043 | #5418. Color the Tree | Inexhaustible# | WA | 14ms | 17396kb | C++14 | 2.9kb | 2024-03-18 17:43:58 | 2024-03-18 17:43:59 |
Judging History
answer
#include <set>
#include <map>
#include <list>
#include <queue>
#include <cmath>
#include <time.h>
#include <random>
#include <bitset>
#include <vector>
#include <cstdio>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mk make_pair
#define fi first
#define se second
inline int read(){
int t = 0,f = 1;
register char c = getchar();
while (c < 48 || c > 57) f = (c == '-') ? (-1) : (f),c = getchar();
while (c >= 48 && c <= 57) t = (t << 1) + (t << 3) + (c ^ 48),c = getchar();
return f * t;
}
const int N = 1e5 + 10,LOGN = 17 + 1;
int T,n,idcnt,a[N],id[N],dep[N],mxdep[N],son[N],top[N],fa[N],dwn[N],lst[N];
int mlg[1 << LOGN],mn[LOGN][N];
ll dp[N];
vector <int> G[N];
void init(){
mlg[0] = -1;
for (int i = 1;i <= n;i++) mlg[i] = mlg[i >> 1] + 1;
for (int i = 0;i < n;i++) mn[0][i] = a[i];
for (int i = 1;i < LOGN;i++){
for (int j = 0;j < n;j++){
mn[i][j] = mn[i - 1][j];
if (j + (1 << (i - 1)) < n) mn[i][j] = min(mn[i][j],mn[i - 1][j + (1 << (i - 1))]);
}
}
}
inline int getmn(int l,int r){
int t = mlg[r - l + 1];
return min(mn[t][l],mn[t][r - (1 << t) + 1]);
}
void dfs0(int u,int f){
mxdep[u] = dep[u] = dep[f] + 1,fa[u] = f;
son[u] = 0;
for (int v : G[u]){
if (v == f) continue;
dfs0(v,u),mxdep[u] = max(mxdep[u],mxdep[v]);
if (mxdep[v] > mxdep[son[u]]) son[u] = v;
}
}
void dfs1(int u,int t){
id[u] = ++idcnt,top[u] = t,dwn[t] = max(dwn[t],id[u]);
if (son[u]) dfs1(son[u],t);
for (int v : G[u]){
if (v == fa[u] || v == son[u]) continue;
dfs1(v,v);
}
}
void upd(int u,int d){
if (lst[u] >= d) return ;
ll v = getmn(lst[u] + 1,d);
dp[u] = min(dp[u],v);
lst[u] = d;
}
void DP(int u){
for (int v : G[u]){
if (v == fa[u]) continue;
DP(v);
}
dp[id[u]] = a[0],lst[id[u]] = 0;
int mxd = 0;
for (int v : G[u]){
if (v == fa[u] || v == son[u]) continue;
mxd = max(mxd,mxdep[v]);
}
for (int v : G[u]){
if (v == fa[u] || v == son[u]) continue;
for (int i = id[v];i <= dwn[v];i++){
dp[id[u] + i - id[v] + 1] += dp[i];
}
}
for (int i = id[u];i <= id[u] + mxd - dep[u];i++){
upd(i,i - id[u]);
}
}
void solve(){
n = read();
for (int i = 0;i < n;i++) a[i] = read();
init();
for (int i = 1;i <= n;i++) G[i].clear(),dwn[i] = 0;
for (int i = 1;i < n;i++){
int u = read(),v = read();
G[u].emplace_back(v),G[v].emplace_back(u);
}
idcnt = 0;
dfs0(1,0);
dfs1(1,1);
DP(1);
ll ans = 0;
for (int i = id[1];i <= dwn[1];i++){
upd(i,i - id[1]);
ans += dp[i];
}
printf("%lld\n",ans);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
// freopen("out.out","w",stdout);
#endif
T = read();
while (T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 17396kb
input:
3 4 10 15 40 1 1 2 2 3 2 4 5 10 5 1 100 1000 1 2 2 3 2 4 4 5 4 1000 200 10 8 1 2 2 3 3 4
output:
35 17 1218
result:
ok 3 number(s): "35 17 1218"
Test #2:
score: -100
Wrong Answer
time: 14ms
memory: 15972kb
input:
3000 54 43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44 1 2 1 3 2 4 3 5 4 6 2 7 4 8 1 9 3 10 1 11 8 12 2 13 9 14 1 15 1 16 15 17 15 18 7 19 1 20 19 21 13 22 10 23 17 24 9 25 9 26 24 27 8 28...
output:
180 134 222 222 156 255 225 126 100 79 139 73 140 127 143 119 228 179 131 171 128 171 78 282 195 286 152 200 119 188 209 213 88 252 207 206 160 179 184 144 109 148 180 169 226 210 182 85 162 59 56 31 115 216 201 144 155 212 53 155 189 134 173 134 216 106 163 255 80 339 156 133 129 156 219 269 148 22...
result:
wrong answer 2nd numbers differ - expected: '168', found: '134'