QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#797725#9806. Growing TreeAiriS#RE 1ms4004kbC++142.8kb2024-12-03 17:13:262024-12-03 17:13:26

Judging History

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

  • [2024-12-03 17:13:26]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:4004kb
  • [2024-12-03 17:13:26]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int N=2100;
int n,len,a[N],vis[N],cut[N];
long long deep[N];
map<long long,vector<int> > las;
int dfs(int u,int nowans){
    //cerr<<u<<' '<<nowans<<'\n';
    if(u==len-(1<<n)){
        return nowans;
    }
    
        while(las[deep[u]].size()){
            int v=las[deep[u]].back();
            int uu=u,vv=v,flag=0;
            while(uu!=vv){
                if(cut[uu]){
                    las[deep[u]].push_back(u);
                    int ans=dfs(u-1,nowans);
                    las[deep[u]].pop_back();
                    return ans;
                }
                else if(cut[vv]){
                    las[deep[u]].pop_back();
                    flag=1;
                    break;
                }
                uu=((uu+1)>>1)-1;
                vv=((vv+1)>>1)-1;
            }
            if(!flag)break;
        }

    if(!las[deep[u]].size()){
        las[deep[u]].push_back(u);
        int ans=dfs(u-1,nowans);
        if(las[deep[u]].back()==u)las[deep[u]].pop_back();
        return ans;
    }
    else{
        int v=las[deep[u]].back();
        //cerr<<u<<' '<<v<<'\n';
        int uu=u,vv=v;
        vector<pair<int,int> > vec;
        while(uu!=vv){
            vec.push_back(make_pair(uu,vv));
            uu=((uu+1)>>1)-1;
            vv=((vv+1)>>1)-1;
        }
        for(int i=vec.size()-1;i>=0;i--)
            if(!vis[i]){
                int ans=-1;
                {
                    int node=vec[i].first;
                    vis[i]=1;
                    cut[node]=1;     
                    las[deep[u]].push_back(u);          
                    ans=dfs(u-1,nowans+1);
                    if(las[deep[u]].back()==u)las[deep[u]].pop_back();
                    cut[node]=0;
                    vis[i]=0;
                }

                {
                    int node=vec[i].second;
                    vis[i]=1;
                    cut[node]=1; 
                    las[deep[u]].push_back(u);              
                    int now=dfs(u-1,nowans+1);
                    if(ans==-1)ans=now;
                    else if(now!=-1)ans=min(ans,now);
                    if(las[deep[u]].back()==u)las[deep[u]].pop_back();
                    cut[node]=0;
                    vis[i]=0;  
                }   
                return ans;
            }
    }
    return -1;
}
int main(){
    int T;scanf("%d",&T);
    while(T--){
        las.clear();
        scanf("%d",&n);
        len=(1<<(n+1))-2;
        for(int i=1;i<=len;i++)   
            scanf("%d",&a[i]);
        for(int i=1;i<=len;i++)
            deep[i]=deep[((i+1)>>1)-1]+a[i];
        cout<<dfs(len,0)<<'\n';
        for(int i=0;i<=n;i++)vis[i]=0;
    }    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4004kb

input:

3
2
1 2 4 3 2 1
2
1 2 3 3 2 1
2
1 2 3 3 1 1

output:

1
2
-1

result:

ok 3 number(s): "1 2 -1"

Test #2:

score: -100
Runtime Error

input:

94
5
44 65 38 61 64 94 71 53 65 10 24 36 98 74 11 4 5 46 72 34 9 24 37 32 76 29 48 88 17 14 36 4 22 6 71 53 24 61 89 79 39 57 99 61 27 85 99 46 81 75 90 25 16 13 1 87 55 81 56 78 67 2
3
83 3 74 14 45 17 22 41 62 74 25 1 56 22
7
21 73 83 99 3 91 16 53 8 10 49 29 54 81 45 10 12 68 32 9 30 11 99 85 73 ...

output:


result: