QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#683037#5418. Color the TreeLaynRE 0ms0kbC++143.1kb2024-10-27 18:28:562024-10-27 18:28:56

Judging History

你现在查看的是最新测评结果

  • [2024-10-27 18:28:56]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-27 18:28:56]
  • 提交

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][25],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][szx].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[0] = 0, dep[0] = -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: 0
Runtime Error

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:


result: