QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#447326#8518. Roars IIImarherCompile Error//C++144.0kb2024-06-18 09:53:102024-06-18 09:53:11

Judging History

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

  • [2024-06-18 09:53:11]
  • 评测
  • [2024-06-18 09:53:10]
  • 提交

answer

#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+50,inf=1e9+7;

struct edge
{
    int u,v,nxt;
}e[N];

int n,head[N],cnt,dep[N],f[N],fa[N],dfn[N],cc,siz[N],p[N],pre[N],val;
char s[N];

void dfs(int x,int F)
{
    fa[x]=F;dep[x]=dep[F]+1;
    dfn[x]=++cc;p[cc]=x;
    siz[x]=1;
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v==F)continue;
        dfs(v,x);siz[x]+=siz[v];
    }
}

#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
#define LS ls,l,mid
#define RS rs,mid+1,r
#define mk(x,y) make_pair(x,y)

int la[N];
pair<int,int>mx[N];

void up(int x)
{
    mx[x]=max(mx[ls],mx[rs]);
}

void build(int x,int l,int r)
{
    if(l==r)
    {
        int u=p[l];
        mx[x]=mk(s[u]?dep[u]:-inf,u);
        return;
    }
    build(LS);build(RS);up(x);
}

void ad(int x,int d)
{
    la[x]+=d;mx[x].first+=d;
}

void down(int x)
{
    if(la[x]==0)return;
    ad(ls,la[x]);ad(rs,la[x]);
    la[x]=0;
}

pair<int,int>find(int x,int l,int r,int L,int R)
{
    if(L>R)return mk(-inf,0);
    if(L<=l&&R>=r)return mx[x];
    down(x);
    pair<int,int>ans;ans.first=-inf;
    if(L<=mid)ans=max(ans,find(LS,L,R));
    if(R>mid)ans=max(ans,find(RS,L,R));
    return ans;
}

void sol(int x,int l,int r,int d,int w)
{
    if(l==r)
    {
        mx[x]=mk(w?(dep[p[l]]+la[x]):-inf,p[l]);
        return;
    }
    down(x);
    if(d<=mid)sol(LS,d,w);
    else sol(RS,d,w);
    up(x);
}

void add(int x,int l,int r,int L,int R,int d)
{
    if(L>R)return;
    if(L<=l&&R>=r)return ad(x,d);
    down(x);
    if(L<=mid)add(LS,L,R,d);
    if(R>mid)add(RS,L,R,d);
    up(x);
}

void sol(int x)
{
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if(v==fa[x])continue;
        sol(v);
    }
    if(s[x])return;
    int l=dfn[x],r=l+siz[x]-1;
    auto pos=find(1,1,n,l,r);
    if(pos.first!=-inf)solve(x,pos.second);
}

int findw(int x,int l,int r,int d)
{
    if(l==r)return la[x]+dep[p[l]];
    down(x);
    if(d<=mid)return findw(LS,d);
    else return findw(RS,d);
}

void solve(int u,int v)
{
    pre[u]=v;
    sol(1,1,n,dfn[v],0);
    sol(1,1,n,dfn[u],1);
    val+=findw(1,1,n,dfn[v])-findw(1,1,n,dfn[u]);
}

void back(int x)
{
    int u=pre[x];pre[x]=0;
    sol(1,1,n,dfn[x],0);
    sol(1,1,n,dfn[u],1);
    val-=findw(1,1,n,dfn[u])-findw(1,1,n,dfn[x]);
}

void solve(int x)
{
    f[x]=val;
    for(int i=head[x];i;i=e[i].nxt)if(e[i].v!=fa[x])
    {
        int u=pre[x],v=e[i].v,l=dfn[v],r=l+siz[v]-1,f=0,c,d;
        if(dfn[u]>=l&&dfn[u]<=r)f=1,back(x);
        int t=pre[v];if(t)back(v);
        add(1,1,n,1,l-1,1);
        add(1,1,n,l,r,-1);
        add(1,1,n,r+1,n,1);
        auto[a,b]=mx[1];
        if(!s[v]&&a>0)
        {
            val+=a-1;pre[v]=b;
            sol(1,1,n,dfn[b],0);
            sol(1,1,n,dfn[v],1);
        }
        if(f)
        {
            auto [cc,dd]=max(find(1,1,n,1,l-1),find(1,1,n,r+1,n));
            c=cc;d=dd;
            if(c<0)f=0;
            else 
            sol(1,1,n,dfn[d],0),
            sol(1,1,n,dfn[x],1),
            val+=c-2;
        }
        solve(v);
        if(f)
        {
            sol(1,1,n,dfn[d],1);
            sol(1,1,n,dfn[x],0);
            val-=c-2;
        }
        if(!s[v]&&a>0)
        {
            sol(1,1,n,dfn[b],1);
            sol(1,1,n,dfn[v],0);
            val-=a-1;pre[v]=0;
        }
        add(1,1,n,1,l-1,-1);
        add(1,1,n,l,r,1);
        add(1,1,n,r+1,n,-1);
        if(t)solve(v,t);
        if(f)solve(x,u);
    }
}

main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    cin>>n>>(s+1);
    for(int i=1;i<=n;i++)s[i]-='0';
    for(int i=1,u,v;i<n;i++)
    {
        cin>>u>>v;
        e[++cnt]=(edge){u,v,head[u]};head[u]=cnt;
        e[++cnt]=(edge){v,u,head[v]};head[v]=cnt;
    }
    dfs(1,0);build(1,1,n);sol(1);solve(1);
    for(int i=1;i<=n;i++)cout<<f[i]<<' ';
}

詳細信息

answer.code: In function ‘void sol(int)’:
answer.code:110:24: error: ‘solve’ was not declared in this scope; did you mean ‘sol’?
  110 |     if(pos.first!=-inf)solve(x,pos.second);
      |                        ^~~~~
      |                        sol
answer.code: In function ‘void solve(int)’:
answer.code:148:13: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  148 |         auto[a,b]=mx[1];
      |             ^
answer.code:157:18: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  157 |             auto [cc,dd]=max(find(1,1,n,1,l-1),find(1,1,n,r+1,n));
      |                  ^
answer.code: At global scope:
answer.code:186:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
  186 | main()
      | ^~~~