QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#539992 | #8134. LCA Counting | PhantomThreshold# | WA | 1ms | 5652kb | C++20 | 1.1kb | 2024-08-31 16:12:27 | 2024-08-31 16:12:31 |
Judging History
answer
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ll long long
//#define int long long
using namespace std;
const int maxn = 310000;
int n;
int fa[maxn];
vector<int>E[maxn];
int f[maxn];
multiset<int>S;
void dfs(const int x)
{
if((int)E[x].size()==0)
{
f[x]=0;
return;
}
if((int)E[x].size()==1)
{
auto y=E[x][0];
f[x]=f[y];
return;
}
for(auto y:E[x]) dfs(y);
vector<int>V;
for(auto y:E[x]) V.push_back(f[y]);
sort(V.begin(),V.end(),greater<int>());
f[x]=V[0]+V[1]+1;
for(int i=2,sz=(int)V.size();i<sz;i++)
S.insert(V[i]);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin>>n;
for(int i=2;i<=n;i++)
{
cin>>fa[i];
E[fa[i]].push_back(i);
}
dfs(1);
int ans=0;
S.insert(f[1]);
auto it=S.end();
vector<int>output;
do
{
ans+=1;
output.push_back(ans);
it--;
for(int j=0;j<(*it);j++)
{
ans+=2;
output.push_back(ans);
}
}while(it!=S.begin());
for(int i=0,sz=(int)output.size();i<sz;i++) cout<<output[i]<<" \n"[i==sz-1];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5584kb
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: 1ms
memory: 5648kb
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: 1ms
memory: 3560kb
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: -100
Wrong Answer
time: 0ms
memory: 5652kb
input:
10 1 2 3 3 3 5 6 4 9
output:
1
result:
wrong answer Answer contains longer sequence [length = 3], but output contains 1 elements