QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#284067 | #5418. Color the Tree | ckain | WA | 15ms | 9712kb | C++14 | 2.4kb | 2023-12-15 22:43:02 | 2023-12-15 22:43:02 |
Judging History
answer
#include<bits/stdc++.h>
#define pii pair<int, int>
#define fr first
#define sc second
#define int long long
using namespace std;
inline int rd(){
int s=0, f=1; char c=getchar();
while(!isdigit(c)) f^=(c=='-'), c=getchar();
while(isdigit(c)) s=s*10+c-'0', c=getchar();
return f? s:-s;
}
void wt(int x, char c=0){
if(x<0) return putchar('-'), wt(-x, c);
if(x>9) wt(x/10); putchar(x%10+'0');
if(c) putchar(c);
}
const int N=1e5+5;
int n, a[N], mxd, anc[N][22], dep[N]={-1}, mx[N];
vector<int> e[N];
int get_lca(int u, int v){
if(dep[v]<dep[u]) swap(u, v);
int dif=dep[v]-dep[u];
for(int i=20; ~i; i--) if(dif>>i&1) u=anc[u][i];
if(u==v) return u;
for(int i=20; ~i; i--) if(anc[u][i]!=anc[v][i]) u=anc[u][i], v=anc[v][i];
return anc[u][0];
}
vector<int> vec[N];
int nxt[N];
namespace ST{
int st[N][22];
void init(){
for(int i=0; i<n; i++) st[i][0]=a[i];
for(int j=1; (1<<j)<=n; j++)
for(int i=0; i+(1<<j)-1<n; i++) st[i][j]=min(st[i][j-1], st[i+(1<<j-1)][j-1]);
}
int que(int l, int r){
int k=__lg(r-l+1);
return min(st[l][k], st[r-(1<<k)+1][k]);
}
}
void solve(){
n=rd();
for(int i=0; i<n; i++) a[i]=rd();
for(int i=1, u, v; i<n; i++){
u=rd(), v=rd();
e[u].push_back(v);
e[v].push_back(u);
}
//init
function<void(int, int)> dfs=[&](int u, int fa){
mx[u]=dep[u]=dep[fa]+1;
mxd=max(mxd, dep[u]);
anc[u][0]=fa;
for(int i=1; (1<<i)<=dep[u]; i++) anc[u][i]=anc[anc[u][i-1]][i-1];
for(int v:e[u]) if(v!=fa) dfs(v, u), mx[u]=max(mx[u], mx[v]);
};
dfs(1, 0);
for(int i=1; i<=n; i++) vec[dep[i]].push_back(i);
nxt[0]=-1;
for(int i=1; i<=mxd; i++){
nxt[i]=0;
for(int j=0; j+1<vec[i].size(); j++){
nxt[i]=max(nxt[i], dep[get_lca(vec[i][j], vec[i][j+1])]);
}
sort(vec[i].begin(), vec[i].end(), [](int u, int v){
return mx[u]>mx[v];
});
}
ST::init();
//
//calc ans
int ans=0;
for(int i=0; i<=mxd; i++){
int cur=1e18;
for(int j=i; ~j; j=nxt[j]){
int l=i-j, r=i-nxt[j]-1;
int L=0, R=vec[j].size();
while(R-L>1){
int mid=(L+R)/2;
if(mx[vec[j][mid]]>=i) L=mid;
else R=mid;
}
cur=min(cur, ST::que(l, r)*(L+1));
}
ans+=cur;
}
//
wt(ans, '\n');
for(int i=0; i<=n; i++){
e[i].clear();
vec[i].clear();
memset(anc[i], 0, sizeof(anc[i]));
}
}
signed main(){
int T=rd();
while(T--){
solve();
}
return 0;
}
/*
1
4
10 15 40 1
1 2
2 3
2 4
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9636kb
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: 15ms
memory: 9712kb
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 174 222 272 156 240 269 165 175 131 171 121 175 150 167 139 254 297 144 191 222 219 103 312 200 295 200 281 144 234 223 249 124 252 351 268 205 265 224 146 199 173 308 199 260 243 248 139 251 141 154 81 184 234 313 235 169 233 108 193 237 235 233 182 281 129 198 291 146 402 188 175 164 223 282 3...
result:
wrong answer 2nd numbers differ - expected: '168', found: '174'