QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#310153#8134. LCA Countingucup-team992#WA 1ms3852kbC++172.0kb2024-01-21 04:13:242024-01-21 04:13:24

Judging History

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

  • [2024-01-21 04:13:24]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3852kb
  • [2024-01-21 04:13:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ar array
#define F first
#define S second
typedef int uci;
#define int long long

void solve(){
    int n;
    cin >> n;
    vector<set<int>> adj(n);
    vector<int> par(n);
    for(int i{1}; i < n; ++i){
        cin >> par[i];
        par[i]--;
        adj[par[i]].insert(i);
    }
    int tot = n;
    for(int i{}; i < n; ++i){
        if(adj[i].size() == 1)
            tot--;
    }

    vector<bool> leaf(n);
    int lc{};
    for(int i{1}; i < n; ++i){
        if(adj[i].size() == 0){
            leaf[i] = true;
            lc++;
            while(par[i] > 0 && adj[par[i]].size() == 1){
                int p = par[i];
                adj[p].erase(adj[p].begin());
                par[i] = par[p];
                adj[par[i]].insert(i);
                adj[par[i]].erase(p);
            }
        }
    }
    vector<int> real(n), fake(n);
    set<ar<int, 2>> nodes;
    for(int i{}; i < n; ++i){
        for(int j : adj[i]){
            if(leaf[j]){
                real[i]++;
            }else
                fake[i]++;
        }
        if(real[i] > 0)
            nodes.insert({-real[i]-fake[i], i});
    }

    vector<int> out(lc, tot);
    for(int i{1}; i < lc; ++i){
        auto a = *nodes.begin();
        nodes.erase(nodes.begin());

        real[a[1]]--;
        a[0] *= -1;
        if(a[0] >= 3){
            tot--;
            if(real[a[1]] > 0){
                nodes.insert({-real[a[1]]-fake[a[1]], a[1]});
            }
        }else{
            tot -= 2;
            while(par[a[1]] > 0 && adj[par[a[1]]].size() == 1){
                int p = par[a[1]];
                par[a[1]] = par[p];
            }
            nodes.erase({-real[par[a[1]]]-fake[par[a[1]]], par[a[1]]});
            real[par[a[1]]]++;
            nodes.insert({-real[par[a[1]]]-fake[par[a[1]]], par[a[1]]});
        }
        out[i] = tot;
    }
    reverse(out.begin(), out.end());
    for(int i : out)
        cout << i << ' ';
    cout << '\n';
}

uci main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 0ms
memory: 3580kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 5 6 7 8 9 

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

10
1 2 2 4 5 6 1 2 4

output:

1 3 5 7 8 

result:

ok 5 number(s): "1 3 5 7 8"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

10
1 2 3 3 3 5 6 4 9

output:

1 3 4 

result:

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

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3852kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...

output:

2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35 37 38 40 41 43 44 46 47 49 50 52 53 55 56 58 59 61 62 64 65 67 68 70 71 73 74 76 77 79 80 82 83 85 86 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 12...

result:

wrong answer 1st numbers differ - expected: '1', found: '2'