QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#158804#7103. Red Black Treeucup-team1448#AC ✓542ms12048kbC++144.6kb2023-09-02 17:02:362023-09-02 17:02:37

Judging History

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

  • [2023-09-02 17:02:37]
  • 评测
  • 测评结果:AC
  • 用时:542ms
  • 内存:12048kb
  • [2023-09-02 17:02:36]
  • 提交

answer

//#define dxx
#ifdef dxx
#define dbg(...) fprintf(stderr,__VA_ARGS__)
#define dex(a) dbg(#a"=%lld onL%d infun %s\n",a,__LINE__,__FUNCTION__)
#include<cstdlib>
#define pause sys##tem("pause")
#define _GLIBCXX_DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<algorithm>

using ll=long long;
using std::max;
using std::min;
template<class T> void cmax(T&a,T b){a=max(a,b);}
template<class T> void cmin(T&a,T b){a=min(a,b);}

namespace gph{
    struct edge{
        int t,w,n;
    } g[200005];
    int cnt=2,hd[100005];
    void ade(int s,int t,int w){
        g[cnt]={t,w,hd[s]};
        hd[s]=cnt++;
    }
    ll oco[100005];
    int contb[100005];
    bool isr[100005];
    void dfs0(int x,int p){
        if(isr[x]){
            oco[x]=0;
            contb[x]=x;
        }
        for(int i=hd[x];i;i=g[i].n) if(g[i].t!=p){
            oco[g[i].t]=oco[x]+g[i].w;
            contb[g[i].t]=contb[x];
            dfs0(g[i].t,x);
        }
    }

    int dfc,dfn[100005],siz[100005],son[100005],dep[100005],\
        pr[100005],top[100005];
    void dfs1(int x,int p){
        siz[x]=1;
        dep[x]=dep[pr[x]=p]+1;
        for(int i=hd[x];i;i=g[i].n) if(g[i].t!=p){
            dfs1(g[i].t,x);
            siz[x]+=siz[g[i].t];
            if(siz[g[i].t]>siz[son[x]]) son[x]=g[i].t;
        }
    }void dfs2(int x,int tp){
        dfn[x]=++dfc;
        top[x]=tp;
        if(son[x]){
            dfs2(son[x],tp);
            for(int i=hd[x];i;i=g[i].n)
                if(g[i].t!=son[x]&&g[i].t!=pr[x])
                    dfs2(g[i].t,g[i].t);
        }
    }int lca(int x,int y){
        while(top[x]!=top[y])
            if(dep[top[x]]<dep[top[y]]) y=pr[top[y]];
            else x=pr[top[x]];
        return dep[x]<dep[y]?x:y;
    }
}

namespace xm{
    int ver[100005];
    void _(){
        int N,M,Q;

        scanf("%d%d%d",&N,&M,&Q);

        gph::cnt=2;
        gph::dfc=0;
        memset(gph::son+1,0,sizeof*gph::son*N);
        memset(gph::hd+1,0,sizeof*gph::hd*N);
        memset(gph::isr+1,0,sizeof*gph::isr*N);

        while(M--){
            int x;
            scanf("%d",&x);
            gph::isr[x]=1;
        }
        for(int i=1;i<N;++i){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            gph::ade(u,v,w);
            gph::ade(v,u,w);
        }

        gph::dfs0(1,0);
        gph::dfs1(1,0);
        gph::dfs2(1,1);

        while(Q--){
            ll ans=-1;
            int K;
            scanf("%d",&K);
            for(int i=1;i<=K;++i) scanf("%d",ver+i);
            std::sort(ver+1,ver+K+1,[&](int x,int y){
                    return gph::oco[x]>gph::oco[y];});
            ans=gph::oco[ver[1]];

            if(K==1){
                puts("0");
                continue;
            }

            //printf("dbg:");
            //for(int i=1;i<=K;++i) printf("%d ",ver[i]);
            //putchar(10);

            if(ans==0){
                puts("0");
                continue;
            }

            int lca=ver[1],j=1;
            ll speupd=-1;
            for(int i=1;i<=K;){
                lca=gph::lca(lca,ver[i]);
                for(int tlca=lca;j<=K&&(tlca=gph::lca(tlca,ver[j]))==lca;++j);
                //printf("lca=%d verj=%d ans=%lld",lca,ver[j],ans);
                //if(j>2) printf(" spe=%lld",gph::oco[ver[2]]-(gph::dep[gph::contb[ver[2]]]<=gph::dep[lca]?gph::oco[lca]:0));
                //putchar(10);
                //cmin(ans,max(max(max(gph::oco[ver[1]]-gph::oco[lca],
                //            j<=K?gph::oco[ver[j]]:0),
                //            j>2?gph::oco[ver[2]]-(gph::dep[gph::contb[ver[2]]]<=gph::dep[lca]?gph::oco[lca]:0):0),
                //            i<K&&j>i+1?gph::oco[ver[i+1]]-(gph::dep[gph::contb[ver[i+1]]]<=gph::dep[lca]?gph::oco[lca]:0):0));
                ll thisupd=gph::oco[ver[1]]-(gph::dep[gph::contb[ver[1]]]<=gph::dep[lca]?gph::oco[lca]:0);

                //printf("thisupd=%d ",thisupd);
                for(int k=i;k<j;++k)
                    if(gph::dep[gph::contb[ver[k]]]>gph::dep[lca]){
                        cmax(speupd,gph::oco[ver[k]]);
                        //printf("spe=%d ",gph::oco[ver[k]]);
                        break;
                    }
                if(j<=K) cmax(thisupd,gph::oco[ver[j]]);
                cmax(thisupd,speupd);
                //printf("now=%d\n",thisupd);

                cmin(ans,thisupd);
                i=j;
            }
            printf("%lld\n",ans);
        }
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--) xm::_();
    return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 5748kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 542ms
memory: 12048kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed