QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#391839 | #8134. LCA Counting | April_19 | WA | 2ms | 11288kb | C++14 | 1.1kb | 2024-04-16 20:37:00 | 2024-04-16 20:37:00 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int read(){
int x=0,f=1;char c;
while(!isdigit(c=getchar()))if(c=='-')f=-1;
while(isdigit(c))x=(x<<1)+(x<<3)+(c&15),c=getchar();
return x*f;
}
const int N=2e5+10;
vector<int>e[N];
int n,leaf,f[N],g[N],s1[N],s2[N],ans[N];
struct node{
int u;
bool operator<(const node &A)const{return f[u]<f[A.u];}
};
priority_queue<node>q;
void dfs(int x){
if(!(int)e[x].size()){
leaf+=f[x]=1;
return;
}
for(int y:e[x]){
dfs(y);
if(f[y]>f[s1[x]])s2[x]=s1[x],s1[x]=y;
else if(f[y]>f[s2[x]])s2[x]=y;
}
f[x]=f[s1[x]]+f[s2[x]];
}
void ins(int x){
for(int y:e[x])if(y!=s1[x]&&y!=s2[x])q.push((node){y});
if(s1[x])ins(s1[x]);
if(s2[x])ins(s2[x]);
}
int main()
{
n=read();
for(int i=2;i<=n;i++){
int x=read();
e[x].pb(i);
}
dfs(1);
ins(1);
int val=f[1],cost=0;
while(q.size()){
int x=q.top().u;
q.pop();
val+=f[x],cost++;
ans[val]=cost;
ins(x);
}
for(int i=1,pre=0;i<=leaf;i++){
pre=max(pre,ans[i]);
ans[i]=pre;
}
for(int i=1;i<=leaf;i++)printf("%d ",2*i-1-ans[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 10424kb
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: 2ms
memory: 9872kb
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: 10344kb
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: 11288kb
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: 0ms
memory: 10644kb
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:
1 3 5 7 9 10 12 13 15 16 18 19 21 22 24 25 27 28 30 31 33 34 36 37 39 40 42 43 45 46 48 49 51 52 54 55 57 58 60 61 63 64 66 67 69 70 72 73 75 76 78 79 81 82 84 85 87 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 5th numbers differ - expected: '8', found: '9'