QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#524326#5665. AA Country and King Dreamoonucup-team3699RE 1ms7756kbC++203.7kb2024-08-19 15:47:332024-08-19 15:47:34

Judging History

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

  • [2024-08-19 15:47:34]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:7756kb
  • [2024-08-19 15:47:33]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define F first 
#define S second 

const int mol=998244353;
const int N = 3e5+5;

vector<int>node[N];
int a[N*2], lst[N], fa[N];
bool vis[N], vv[N], in[N];
vector<int>un;
int iu=0;
void dfs(int v, int pre){
    vv[v]=1;
    iu++;
    a[iu]=v;
    if(a[iu+1]==pre) return;
    while(a[iu+1]){ dfs(a[iu+1], fa[a[iu+1]]), iu++;if(!a[iu]) a[iu]=v;if(a[iu+1]==pre) return;}
    int nn=-1;
    for(int i: node[v]){
        if(i==pre) continue;
        if(!in[i]) nn=i; 
    }
    if(v==pre){
        if(un.size()==0&&nn!=-1){
            dfs(nn, v);
            iu++;a[iu]=v;
            return;
        }
        while(un.size()>=2&&un[un.size()-2]<v){
            if(nn!=-1&&un.back()>nn){
                dfs(nn, v);
                iu++;a[iu]=v;
                break;
            }
            a[++iu]=un.back(), un.pop_back(), a[++iu]=un.back(), un.pop_back(), a[++iu]=a[iu-2];a[++iu]=v;
        }
        while(un.size()) {
            if(nn!=-1&&!vv[nn]&&nn<un.back()) {dfs(nn, v);iu++;break;}
            a[++iu]=un.back(), a[++iu]=v, un.pop_back();
        }
        if(!vv[nn]) dfs(nn, v), iu++, a[iu]=v;
        return;
    }
        while(un.size()&&un.back()<pre){
            if(nn!=-1&&un.back()>nn){
                dfs(nn, v);
                iu++;
                a[iu]=v;
                break;
            }
            a[iu++]=un.back();un.pop_back();
            if(un.size()&&un.back()<v) 
                a[iu++]=un.back(), un.pop_back(),
                a[iu++]=a[iu-2], a[iu++]=v;
            else a[iu++]=v;
        }
        if(lst[v]>iu){
            while(un.size()>=2&&un[un.size()-2]<v){
                if(nn!=-1&&!vv[nn]&&un.back()>nn){
                    dfs(nn, v);
                    iu++;a[iu]=v;
                    break;
                }
                a[++iu]=un.back(), un.pop_back(), a[++iu]=un.back(), un.pop_back(), a[++iu]=a[iu-2];a[++iu]=v;
            }
            while(un.size()) {
                if(nn!=-1&&!vv[nn]&&un.back()>nn){
                    dfs(nn, v);
                    iu++;a[iu]=v;
                    break;
                }
                a[++iu]=un.back(), a[++iu]=v, un.pop_back();
            }
            vector<pair<int, int>>ha;
            for(int i: node[v]){
                if(!vv[i]) ha.pb({lst[i], i});
            }
            sort(ha.begin(), ha.end());
            for(auto t: ha){
                dfs(t.S, v);
            }
        }
        else return;
    
}
void d(int v, int pre){
    fa[v]=pre;
    for(int k: node[v]){
        if(k==pre) continue;
        d(k, v);
    }
}

void solve(){
    int n;
    iu=0;
    cin>>n;

    for(int i=1;i<=n;i++) vis[i]=0, node[i].clear(), vv[i]=0, lst[i]=0, in[i]=0, fa[i]=0;
    un.clear();
    a[n*2]=0;
    for(int i=1;i<=n*2-1;i++){
        cin>>a[i];
        lst[a[i]]=i;
        vis[a[i]]=1;
        if(i>1&&a[i-1]&&a[i]){
            node[a[i-1]].pb(a[i]);
            node[a[i]].pb(a[i-1]);
        }
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]) un.pb(i);
    }
    reverse(un.begin(), un.end());
    int r;
    if(!a[1]&&!a[n*2-1]){
        for(int i=2;i<=n;i++) cout<<"1 "<<i<<" ";
        cout<<"1\n";
        return;
    }
    else r=max(a[1], a[n*2-1]); 
    a[1]=r, a[n*2-1]=r;
    d(r, r);
    for(int i=2;i<=n;i++){
        if(a[i-1]==fa[a[i]]&&a[i-1]&&a[i]) in[a[i]]=1;
    }
    dfs(r, r);
    for(int i=1;i<=n*2-1;i++) cout<<a[i]<<" ";cout<<"\n";
}

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

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

result:

ok 9 lines

Test #2:

score: -100
Runtime Error

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:

1 2 1 
1 2 1 
1 2 1
1 2 1 
1 2 1 
1 2 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2...

result: