QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#225570#5665. AA Country and King DreamoonBUET_TwilightWA 37ms13872kbC++144.1kb2023-10-24 20:03:182023-10-24 20:03:19

Judging History

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

  • [2023-10-24 20:03:19]
  • 评测
  • 测评结果:WA
  • 用时:37ms
  • 内存:13872kb
  • [2023-10-24 20:03:18]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define setbit0(n, i) ((n) & (~(1LL << (i))))
#define setbit1(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define lastone(n) ((n) & (-(n)))
char gap = 32;
#define int long long
#define ll long long
#define lll __int128_t
#define pb push_back
// #define double long double
#define pii pair<int,int>
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;
typedef pair<double, double> pdd;
const double EPS = 1e-12;
#define endl '\n'

const int N = 300005;

vector<int> adj[N];
int pres[N];
int parent[N];
void dfs(int u,int p){
    for(auto v:adj[u]){
        if( v == p ) continue;
        parent[v] = u;
        dfs(v,u);
    }
}
void solve(){
    int n;
    cin>>n;

    for(int i=0;i<=n;i++) {
        adj[i].clear();
        pres[i] = 0;
    }
    pres[1] = 1;
    vector<int> prev,last;
    bool hasZero = false;

    for(int i=0;i<2*n-1;i++){
        int a;
        cin>>a;
        pres[a] = 1;
        if( a == 0 ){
            hasZero = true;
            continue;
        }
        if( !hasZero ) prev.push_back(a);
        else last.push_back(a);
    }
    if( last.size() == 0 ) last.push_back(1);
    if( prev.size() == 0 ) prev.push_back(1);

    for(int i=1;i<prev.size();i++){
        adj[prev[i-1]].push_back(prev[i]);
        adj[ prev[i] ].push_back(prev[i-1]);
    }
    for(int i=1;i<last.size();i++){
        adj[last[i-1]].push_back(last[i]);
        adj[ last[i] ].push_back(last[i-1]);
    }

    dfs(1,0);


    map<int,int> mp;
    for(int i=0;i<last.size();i++){
        mp[last[i]] = i;
    }
    int root = 1;
    int pos = last.size()-1;
    for(int i=prev.size()-1;i>=0;i--){
        if( mp.find(prev[i]) != mp.end() ){
            int cur = mp[prev[i]];
            if( cur<pos) {
                pos = cur;
                root = prev[i];
            }
        }
    }

    //cout<<root<<endl;

    vector<int> abs;
    for(int i=1;i<=n;i++) if( !pres[i] ) abs.push_back(i);

    vector<int> path;
    int curr = prev.back();
    while(curr){
        path.push_back(curr);
        curr = parent[curr];
    }
    reverse(path.begin(),path.end());

    //for(auto p:path) cout<<p<<" ";
    //cout<<endl;
    int cur = path.size()-1;
    for(auto a:abs){
        while( true  ){
            if( path.back() == root ) break;
            if( path[path.size()-2] > a ) break;
            path.pop_back();
            prev.push_back(path.back());
        }
       // cout<<path.back()<<"->"<<a<<endl;
       parent[a] = path.back();
        adj[path.back()].push_back(a);
        adj[a].push_back(path.back());
        path.push_back(a);
        prev.push_back(a);
    }
    path.clear();
    curr = prev.back();
    while(curr){
        path.push_back(curr);
        curr = parent[curr];
    }
    //reverse(path.begin(),path.end());
    vector<int> path2;
    curr = last[0];
    while(curr){
        path2.push_back(curr);
        curr = parent[curr];
    }
    //reverse(path2.begin(),path2.end());
    //for(auto p:path) cout<<p<<" "; cout<<endl;
    //for(auto p:path2) cout<<p<<" "; cout<<endl;
    while(path.size() and path2.size() and path.back() == path2.back()) {
        path.pop_back();
        path2.pop_back();
    }
    for(auto p:prev) cout<<p<<" ";

    reverse(path.begin(),path.end());
    for(int i=1;i<path.size();i++) cout<<path[i]<<" ";
    if( last[0] != root and prev.back() != root )
    cout<<root<<" ";
    for(int i=1;i<path2.size();i++) cout<<path2[i]<<" ";
    for(auto l:last) cout<<l<<" ";
    cout<<endl;

}
signed main(){

     ios_base::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
    int tc;
    cin>>tc;
    while(tc--)
    solve();

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 13872kb

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
Wrong Answer
time: 37ms
memory: 13648kb

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 3 1 2 1 
1 2 3 2 1 
1 3 1 2 1 
1 2 3 2 1 
1 2 3 3 1 
1 2 3 3 1 
1 2 3...

result:

wrong answer 24th lines differ - expected: '1 2 3 2 1', found: '1 3 1 2 1 '