QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#538103#8134. LCA CountingIstleminWA 0ms3620kbC++141.7kb2024-08-31 00:31:172024-08-31 00:31:18

Judging History

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

  • [2024-08-31 00:31:18]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3620kb
  • [2024-08-31 00:31:17]
  • 提交

answer

#include<bits/stdc++.h>

#pragma GCC optimize ("Ofast")

using namespace std;

#define rep(i,a,b) for(ll i = a; i<b;i++)
#define rrep(i,a,b) for(ll i = b-1; i>=a;i--)
#define trav(x,a) for(auto &x: a)
#define all(v) v.begin(),v.end()
#define sz(v) ll(v.size())

typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    ll n; cin>>n;

    vector<vl> e(n);
    rep(i,1,n) {
        ll p; cin>>p;
        e[p-1].push_back(i);
    }

    ll leaves = 0;

    vl mem(n);
    
    function<ll(ll)> rec = [&](ll v) {
        if(sz(e[v])==0){
            leaves ++;
            return mem[v] = 1ll;
        }
        vl chAns;
        for(auto u: e[v]){
            chAns.push_back(rec(u));
        }
        sort(all(chAns));
        ll ans = chAns.back();
        if(sz(chAns)>1) ans += chAns[sz(chAns)-2];
        return mem[v] = ans;
    };

    //ll goods = rec(0);
    vl ans;

    function<void(ll)> genans = [&](ll v) {
        if(sz(e[v])==0){
            return;
        }
        vector<pll> chAns;
        for(auto u: e[v]){
            chAns.emplace_back(mem[u],u);
        }
        if(sz(chAns)==1) {
            genans(chAns.back().second);
        }
        if(sz(chAns)>1) {
            sort(all(chAns));
            genans(chAns.back().second);
            ans.push_back(2);
            genans(chAns[sz(chAns)-2].second);
        }
        rep(i,0,sz(chAns)-2){
            ans.push_back(1);
            genans(chAns[i].second);
        }
        return;
    };

    genans(0);

    ll a = 1;
    cout<<1<<" ";
    rep(i,0,sz(ans)){
        a += ans[i];
        cout<<a<<" ";
    }
    cout<<endl;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3616kb

input:

7
1 1 2 4 2 2

output:

1 3 5 6 

result:

ok 4 number(s): "1 3 5 6"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3620kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 4 6 7 8 9 

result:

wrong answer 3rd numbers differ - expected: '5', found: '4'