QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#538109 | #8134. LCA Counting | Istlemin | WA | 0ms | 3812kb | C++14 | 1.8kb | 2024-08-31 01:17:30 | 2024-08-31 01:17:30 |
Judging History
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;
};
rec(0);
set<pll> q;
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);
}
rep(i,0,sz(chAns)){
if(i>=sz(chAns)-2){
genans(chAns[i].second);
} else {
q.emplace(chAns[i].first,chAns[i].second);
}
}
return;
};
q.emplace(mem[0],0);
vl ans;
while(q.size()){
auto [g,v] = *prev(q.end());
q.erase(prev(q.end()));
genans(v);
ans.push_back(g);
}
ll a = 0;
for(auto x: ans){
a += 1;
cout<<a<<" ";
rep(i,0,x-1){
a += 2;
cout<<a<<" ";
}
}
cout<<endl;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3812kb
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: 3548kb
input:
10 1 1 2 2 1 1 1 2 4
output:
1 3 5 6 8 9 10 11
result:
wrong answer 5th numbers differ - expected: '7', found: '8'