QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#683002 | #5418. Color the Tree | Layn | WA | 24ms | 18720kb | C++14 | 3.1kb | 2024-10-27 18:20:34 | 2024-10-27 18:20:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int S=200005;
int e,siz[S],dep[S],fa[S],DEEP[S],link[S],head[S],to[S],son[S],n;
void dfs(int u,int FA) {
siz[u]=1,fa[u]=FA,dep[u]=dep[FA]+1,DEEP[u]=dep[u];
for(int i=head[u];i;i=link[i]) {
int v=to[i];
if(v==FA)continue;
dfs(v,u);
siz[u]+=siz[v];
DEEP[u]=max(DEEP[u],DEEP[v]);
if(DEEP[son[u]]<DEEP[v])son[u]=v;
}
}
int a[S],link_id[S];
vector<pair<long long, pair<int, int> > > ve[S];
int ST[S][20],i2[S];
int Qry(int l,int r) {
return min(ST[l][i2[r-l+1]], ST[r-(1<<i2[r-l+1])+1][i2[r-l+1]]);
}
const int INF = 1e9;
void Clr(int x, int cur) {
int L = INF, R = -INF, siz = ve[x].size();
for (int i = siz - 1; i >= siz - cur; i--) {
if (L != INF) {
L++, R++;
}
if (ve[x][i].second.first != -1) {
L = min(L, ve[x][i].second.first);
R = max(R, ve[x][i].second.second);
ve[x][i].second = make_pair(-1, -1);
}
if (L == INF) continue;
ve[x][i].first = min(ve[x][i].first, Qry(L, R)*1ll);
}
if (L != INF && siz - cur - 1 > 0)
ve[x][siz - cur - 1].second = make_pair(L, R);
}
int dfs1(int u) {
if (son[u]) {
link_id[u] = dfs1(son[u]);
} else {
link_id[u] = u;
}
int x = link_id[u];
int cur=0;
ve[x].push_back(make_pair(a[0], make_pair(-1, -1)));
for (int i=head[u];i;i=link[i])
if (to[i]!=son[u] && to[i]!=fa[u]) {
cur=max(cur,DEEP[to[i]]);
}
cur = cur - dep[u] + 1;
//cur++;
Clr(x, cur);
int szx = ve[x].size();
for (int i=head[u];i;i=link[i])
if (to[i]!=son[u] && to[i]!=fa[u]) {
int o = dfs1(to[i]);
Clr(o, ve[o].size());
for (int sz=ve[o].size(),j=sz-1;j>=0;--j) {
ve[x][szx-(sz-j)-1].first += ve[o][j].first;
//ve[x][DEEP[u]-DEEP[to[i]]+j].first += ve[o][j].first;
}
ve[o].clear();
}
ve[x][DEEP[u] - dep[u]].second = make_pair(0, 0);
//ve[x][DEEP[u]].second = make_pair(0, 0);
return x;
}
void add(int uu,int vv) {
to[++e]=vv;
link[e]=head[uu];
head[uu]=e;
}
int main() {
int T = 0;
scanf("%d",&T);
i2[1]=0;
for (int i=2;i<S;++i)
i2[i]=i2[i>>1]+1;
while(T--) {
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&a[i]),ST[i][0]=a[i];
e=0;
for(int i=1;i<=n;i++)head[i]=0,ve[i].clear(), son[i] = 0, link_id[i] = 0;
for (int i=1;(1<<i)<=n;++i)
for (int j=0;j+(1<<i)-1<n;++j)
ST[j][i]=min(ST[j][i-1],ST[j+(1<<(i-1))][i-1]);
for(int i=1,u,v;i<n;i++)scanf("%d%d",&u,&v),add(u,v),add(v,u);
//printf("NMSL---------\n");
DEEP[1] = 1, dfs(1,0);
//printf("NMSL---------\n");
int x = dfs1(1);
//printf("NMSL---------\n");
Clr(x, ve[x].size());
long long ans = 0;
for (int i = 0; i < ve[x].size(); i++)
ans += ve[x][i].first;
printf("%lld\n", ans);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18720kb
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: 24ms
memory: 17016kb
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 230 143 241 225 115 100 81 155 73 147 129 149 124 219 230 132 225 142 170 77 282 195 286 188 211 119 197 211 227 88 249 244 235 173 179 195 120 109 148 180 175 226 210 182 85 211 59 56 31 115 204 201 163 139 217 53 140 189 169 173 137 233 98 163 273 80 350 156 133 134 159 240 269 137 221...
result:
wrong answer 2nd numbers differ - expected: '168', found: '174'