提出 #72825096


ソースコード 拡げる

#include<bits/stdc++.h>
#define ll long long
#define eb emplace_back
#define ep emplace
#define pii pair<int,int>
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define mems(arr,x) memset(arr,x,sizeof(arr))
#define memc(arr1,arr2) memcpy(arr1,arr2,sizeof(arr2))
using namespace std;
const int maxn=1e5+10,inf=1e9+10;
const ll INF=1e18+10;
int n,tot;
int a[maxn];
int fa[maxn],L[maxn],R[maxn],rnk[maxn],deg[maxn];
int UL[maxn],UR[maxn],Urnk[maxn],siz[maxn],son[maxn],id[maxn<<2];
ll ans[maxn];
const int maxp=maxn<<1,maxe=maxp<<4;
struct GraphFlow{
    #define GO(x,i) for(int i=head[x],t=e[i].to,w=e[i].w,co=e[i].co;i;i=e[i].nxt,t=e[i].to,w=e[i].w,co=e[i].co)
    int S,T;
    int cnt=1;
    int head[maxp];
    struct edge{int nxt,to,w,co;}e[maxe];
    inline void add(int u,int v,int w,int co){e[++cnt]={head[u],v,w,co};head[u]=cnt;}
    inline void adde(int u,int v,int w,int co){add(u,v,w,co);add(v,u,0,-co);}
    inline void rebuild(){for(int i=0;i<=T;i++)head[i]=0;cnt=1;S=T=0;}
    ll d[maxp];
    int pre[maxp],flw[maxp];
    bool inq[maxp];
    queue<int> q;
    inline bool bfs(){
        for(int i=0;i<=T;i++)   d[i]=INF,flw[i]=inf;
        d[S]=pre[T]=0;inq[S]=true;q.ep(S);
        while(!q.empty()){
            int u=q.front();q.pop();inq[u]=false;
            GO(u,i){
                if(!w||d[t]<=d[u]+co)  continue;
                d[t]=d[u]+co;pre[t]=i;
                flw[t]=min(flw[u],w);
                if(!inq[t]){inq[t]=true;q.ep(t);}
            }
        }
        return pre[T];
    }
    inline ll SSP(){
        ll cost=0;int tim=4;
        while(tim--){
            if(!bfs())  return -1;
            cost+=flw[T]*d[T];
            for(int p=T;p!=S;p=e[pre[p]^1].to){
                e[pre[p]].w-=flw[T];e[pre[p]^1].w+=flw[T];
            }
        }
        return cost;
    }
}F;
const int maxm=maxn<<1;
struct Graph{
    #define go(tr,x,i) for(int i=tr.head[x],t=tr.e[i].to;i;i=tr.e[i].nxt,t=tr.e[i].to)
    int cnt=1;
    int head[maxn];
    struct edge{int nxt,to;}e[maxm];
    inline void add(int u,int v){e[++cnt]={head[u],v};head[u]=cnt;}
    inline void adde(int u,int v){add(u,v);add(v,u);}
    inline void rebuild(){for(int i=0;i<=n;i++)head[i]=0;cnt=1;}
}T,U;
namespace SegmentTree{
    #define ls p<<1
    #define rs p<<1|1
    #define lson ls,l,mid
    #define rson rs,mid+1,r
    #define all 1,1,n
    #define setmid int mid=(l+r)>>1
    #define setpos int p,int l,int r
    pii tmp[8];int pos[maxn];
    struct tree{
        pii a[4];
        tree(){a[0]=a[1]=a[2]=a[3]=pii(inf,-1);}
        pii& operator[](int x){return a[x];}
        tree operator+(tree y){
            tree res=tree();
            merge(a,a+4,y.a,y.a+4,tmp);
            int len=0;
            for(int i=0;;i++){
                res[len++]=tmp[i];
                if(~tmp[i].se){for(int j=0;j<len-1;j++)if(res[j].se==tmp[i].se){len--;break;}}
                if(len==4)return res;
            }
            return res;
        }
    }tr[maxn<<2];
    inline void pu(int p){tr[p]=tr[ls]+tr[rs];}
    void build(setpos){if(l==r)return pos[l]=p,tr[p]=tree(),tr[p][0]=pii(a[rnk[l]],0),void();setmid;build(lson);build(rson);pu(p);}
    inline void upd(int x,int col){int p=pos[x];tr[p]=tree();tr[p][0]=pii(a[rnk[x]],col);while(p>>=1)pu(p);}
    tree query(setpos,int pl,int pr){if(l>=pl&&r<=pr)return tr[p];setmid;tree res=tree();if(pl<=mid)res=query(lson,pl,pr);if(pr>mid)res=res+query(rson,pl,pr);return res;}
    void print(setpos){if(l==r)return printf("col[%lld] = %lld\n",rnk[l],tr[p][0].se),void();setmid;print(lson);print(rson);}
}
using namespace SegmentTree;
void dfs1(int u,int ft){fa[u]=ft;L[u]=++tot;rnk[tot]=u;go(T,u,i)if(t^ft)dfs1(t,u);R[u]=tot;}
void dfs2(int u,int ft){
    son[u]=0;siz[u]=1;
    UL[u]=++tot;Urnk[tot]=u;
    go(U,u,i)if(t^ft){
        dfs2(t,u);
        if(siz[t]>siz[son[u]])  son[u]=t;
        siz[u]+=siz[t];
    }
    UR[u]=tot;
}
vector<int> clr;
void dfs3(int u,int ft,int col){
    go(U,u,i)if((t^ft)&&(t^son[u])){
        dfs3(t,u,++tot);
        for(int i=UL[t];i<=UR[t];i++)upd(L[Urnk[i]],0);
        tot--;
    }
    if(son[u])  dfs3(son[u],u,col);
    go(U,u,_)if((t^ft)&&(t^son[u])){
        tot++;
        for(int i=UL[t];i<=UR[t];i++)upd(L[Urnk[i]],tot);
    }
    {
        int cid=deg[u],cur=0;vector<int>().swap(clr);F.rebuild();
        go(T,u,_){
            cur++;
            tree res;
            if(t^fa[u]) res=query(all,L[t],R[t]);
            else{
                res=query(all,1,L[u]-1);
                if(R[u]^n)  res=res+query(all,R[u]+1,n);
            }
            for(int i=0;i<4;i++){
                int w=res[i].fi,c=res[i].se;
                if(!~c)break;
                if(!id[c])    id[c]=++cid,clr.eb(c);
                F.adde(cur,id[c],1,w);
            }
        }
        F.S=0;F.T=++cid;
        if(cid-deg[u]<4)    ans[u]=-1;
        else{
            for(int i=1;i<=cur;i++) F.adde(F.S,i,1,0);
            for(int i=cur+1;i<=cid;i++) F.adde(i,F.T,1,0);
            ans[u]=F.SSP();
        }
        deg[u]=0;for(int i:clr)id[i]=0;
    }
    upd(L[u],col);
    go(U,u,_)if((t^ft)&&(t^son[u])){
        for(int i=UL[t];i<=UR[t];i++)upd(L[Urnk[i]],col);
    }
}
void matt(){
    T.rebuild();U.rebuild();tot=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)   scanf("%d",&a[i]);
    for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);T.adde(u,v);deg[u]++;deg[v]++;}
    for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);U.adde(u,v);}
    dfs1(1,0);tot=0;dfs2(1,0);tot=0;build(all);dfs3(1,0,++tot);
    for(int i=1;i<=n;i++)   printf("%lld ",ans[i]);puts("");
}
main(){int T;scanf("%lld",&T);while(T--)matt();}

提出情報

提出日時
問題 C - Double X
ユーザ yaohaoyou
言語 C++23 (GCC 15.2.0)
得点 900
コード長 5800 Byte
結果 AC
実行時間 1297 ms
メモリ 43008 KiB

コンパイルエラー

./Main.cpp: In function 'void SegmentTree::print(int, int, int)':
./Main.cpp:98:54: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'int' [-Wformat=]
   98 |     void print(setpos){if(l==r)return printf("col[%lld] = %lld\n",rnk[l],tr[p][0].se),void();setmid;print(lson);print(rson);}
      |                                                   ~~~^            ~~~~~~
      |                                                      |                 |
      |                                                      long long int     int
      |                                                   %d
./Main.cpp:98:62: warning: format '%lld' expects argument of type 'long long int', but argument 3 has type 'int' [-Wformat=]
   98 |     void print(setpos){if(l==r)return printf("col[%lld] = %lld\n",rnk[l],tr[p][0].se),void();setmid;print(lson);print(rson);}
      |                                                           ~~~^
      |                                                              |
      |                                                              long long int
      |                                                           %d
./Main.cpp: In function 'void matt()':
./Main.cpp:162:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  162 |     for(int i=1;i<=n;i++)   printf("%lld ",ans[i]);puts("");
      |     ^~~
./Main.cpp:162:52: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  162 |     for(int i=1;i<=n;i++)   printf("%lld ",ans[i]);puts("");
      |                                                    ^~~~
./Main.cpp: At global scope:
./Main.cpp:164:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  164 | main(){int T;scanf("%lld",&T);while(T--)matt();}
      | ^~~~
./Main.cpp: In function 'int main()':
./Main.cpp:164:24: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int*' [-Wformat=]
  164 | main(){int T;scanf("%lld",&T);while(T--)matt();}
      |                     ~~~^  ~~
      |                        |  |
      |                        |  int*
      |                        long long int*
      |                     %d

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 900 / 900
結果
AC × 1
AC × 66
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 03_same_00.txt, 03_same_01.txt, 03_same_02.txt, 03_same_03.txt, 04_almost_same_00.txt, 04_almost_same_01.txt, 04_almost_same_02.txt, 04_almost_same_03.txt, 05_many_deg_v_is_4_00.txt, 05_many_deg_v_is_4_01.txt, 05_many_deg_v_is_4_02.txt, 05_many_deg_v_is_4_03.txt, 06_many_high_degree_v_00.txt, 06_many_high_degree_v_01.txt, 06_many_high_degree_v_02.txt, 06_many_high_degree_v_03.txt, 06_many_high_degree_v_04.txt, 06_many_high_degree_v_05.txt, 06_many_high_degree_v_06.txt, 06_many_high_degree_v_07.txt, 06_many_high_degree_v_08.txt, 06_many_high_degree_v_09.txt, 06_many_high_degree_v_10.txt, 07_high_deg_v1_00.txt, 07_high_deg_v1_01.txt, 07_high_deg_v1_02.txt, 08_ternary_00.txt, 08_ternary_01.txt, 08_ternary_02.txt, 09_path_00.txt, 09_path_01.txt, 09_path_02.txt, 10_centipede_00.txt, 10_centipede_01.txt, 10_centipede_02.txt, 10_centipede_03.txt, 10_centipede_04.txt, 10_centipede_05.txt, 11_star_00.txt, 11_star_01.txt, 12_ternary_2_00.txt, 12_ternary_2_01.txt, 13_hack_00.txt, 13_hack_01.txt, 13_hack_02.txt, 13_hack_03.txt, 13_hack_04.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 6 ms 16500 KiB
01_small_00.txt AC 138 ms 16528 KiB
01_small_01.txt AC 138 ms 16608 KiB
01_small_02.txt AC 226 ms 16572 KiB
01_small_03.txt AC 226 ms 16560 KiB
01_small_04.txt AC 373 ms 16480 KiB
01_small_05.txt AC 372 ms 16480 KiB
02_random_00.txt AC 823 ms 23228 KiB
02_random_01.txt AC 1030 ms 25916 KiB
02_random_02.txt AC 682 ms 19372 KiB
02_random_03.txt AC 1002 ms 25820 KiB
02_random_04.txt AC 730 ms 20848 KiB
02_random_05.txt AC 997 ms 26072 KiB
02_random_06.txt AC 894 ms 24176 KiB
02_random_07.txt AC 987 ms 26032 KiB
02_random_08.txt AC 945 ms 25344 KiB
02_random_09.txt AC 1003 ms 26072 KiB
02_random_10.txt AC 787 ms 21936 KiB
02_random_11.txt AC 969 ms 26052 KiB
03_same_00.txt AC 665 ms 25904 KiB
03_same_01.txt AC 648 ms 25968 KiB
03_same_02.txt AC 641 ms 25972 KiB
03_same_03.txt AC 636 ms 25952 KiB
04_almost_same_00.txt AC 668 ms 26052 KiB
04_almost_same_01.txt AC 641 ms 26080 KiB
04_almost_same_02.txt AC 652 ms 25860 KiB
04_almost_same_03.txt AC 637 ms 25900 KiB
05_many_deg_v_is_4_00.txt AC 1288 ms 25820 KiB
05_many_deg_v_is_4_01.txt AC 826 ms 26072 KiB
05_many_deg_v_is_4_02.txt AC 1297 ms 26052 KiB
05_many_deg_v_is_4_03.txt AC 824 ms 25904 KiB
06_many_high_degree_v_00.txt AC 809 ms 25864 KiB
06_many_high_degree_v_01.txt AC 684 ms 26024 KiB
06_many_high_degree_v_02.txt AC 499 ms 26156 KiB
06_many_high_degree_v_03.txt AC 764 ms 26172 KiB
06_many_high_degree_v_04.txt AC 575 ms 26356 KiB
06_many_high_degree_v_05.txt AC 802 ms 25972 KiB
06_many_high_degree_v_06.txt AC 710 ms 26080 KiB
06_many_high_degree_v_07.txt AC 929 ms 26032 KiB
06_many_high_degree_v_08.txt AC 824 ms 26000 KiB
06_many_high_degree_v_09.txt AC 501 ms 26116 KiB
06_many_high_degree_v_10.txt AC 936 ms 25788 KiB
07_high_deg_v1_00.txt AC 502 ms 31164 KiB
07_high_deg_v1_01.txt AC 512 ms 30984 KiB
07_high_deg_v1_02.txt AC 509 ms 31020 KiB
08_ternary_00.txt AC 815 ms 25984 KiB
08_ternary_01.txt AC 815 ms 25984 KiB
08_ternary_02.txt AC 816 ms 26080 KiB
09_path_00.txt AC 1001 ms 28288 KiB
09_path_01.txt AC 597 ms 43008 KiB
09_path_02.txt AC 397 ms 42924 KiB
10_centipede_00.txt AC 984 ms 27120 KiB
10_centipede_01.txt AC 620 ms 34480 KiB
10_centipede_02.txt AC 415 ms 34544 KiB
10_centipede_03.txt AC 974 ms 27400 KiB
10_centipede_04.txt AC 347 ms 37424 KiB
10_centipede_05.txt AC 242 ms 37600 KiB
11_star_00.txt AC 252 ms 40180 KiB
11_star_01.txt AC 251 ms 40100 KiB
12_ternary_2_00.txt AC 829 ms 25860 KiB
12_ternary_2_01.txt AC 821 ms 26052 KiB
13_hack_00.txt AC 712 ms 26000 KiB
13_hack_01.txt AC 737 ms 25900 KiB
13_hack_02.txt AC 728 ms 25900 KiB
13_hack_03.txt AC 728 ms 25984 KiB
13_hack_04.txt AC 741 ms 26044 KiB