QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#763475 | #9751. 覆盖一棵树 | tianxiawoyou# | WA | 27ms | 9024kb | C++14 | 834b | 2024-11-19 20:29:23 | 2024-11-19 20:29:23 |
Judging History
answer
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int N=200005;
vector<int>e[N];
int dep[N],mx[N],mn[N];
int t,n,k,ans;
void dfs(int x,int depth){
dep[x]=depth;if(!e[x].size())mx[x]=mn[x]=depth;
for(int y:e[x])dfs(y,depth+1);
}
void dfs(int x){
for(int y:e[x])dfs(y);
for(int y:e[x]){
mx[x]=max(mx[x],mx[y]);
mn[x]=min(mn[x],mn[y]);
}
// cerr<<x<<" "<<mx[x]<<" "<<mn[x]<<"\n";
ans=max(ans,max(mx[x]-dep[x],mn[x]));
}
int main(){
// freopen("1.in","r",stdin);
cin>>t;
while(t--){
ans=0;cin>>n;
for(int i=1;i<=n;++i)
dep[i]=0,mx[i]=0,mn[i]=INF,e[i].clear();
for(int i=2,x;i<=n;++i)cin>>x,e[x].push_back(i);
dfs(1,0);
dfs(1);
cout<<ans<<"\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 8780kb
input:
2 8 1 2 3 2 5 1 7 8 1 2 3 4 5 6 7
output:
3 7
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 27ms
memory: 9024kb
input:
33428 10 1 2 3 3 4 6 7 7 9 10 1 2 3 4 5 6 7 8 8 8 1 2 3 4 5 6 7 8 1 2 3 4 4 6 7 4 1 2 3 3 1 2 3 1 1 9 1 2 3 4 5 6 7 8 2 1 3 1 2 10 1 2 3 4 5 6 7 8 9 3 1 2 2 1 10 1 2 3 4 5 6 7 8 9 2 1 5 1 2 2 4 8 1 2 3 4 5 6 7 5 1 2 3 3 2 1 5 1 2 3 4 3 1 2 9 1 2 3 4 5 6 6 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 5 7 8 8 1 2 ...
output:
7 8 7 6 3 2 1 8 1 2 9 2 1 9 1 3 7 3 1 4 2 7 8 7 6 4 1 2 9 7 4 3 4 6 7 3 5 7 6 5 9 3 4 4 8 2 8 6 2 4 8 4 2 1 5 2 5 2 4 7 2 2 5 2 2 6 3 9 2 5 7 4 2 2 1 3 1 9 1 5 1 7 3 6 2 9 7 2 3 1 3 3 1 2 8 6 8 4 7 7 7 6 5 7 8 4 5 2 5 5 1 7 7 7 2 2 2 3 8 3 5 6 4 3 3 3 2 2 4 8 6 7 8 6 4 2 4 7 8 1 6 2 7 7 9 1 2 8 5 2 ...
result:
wrong answer 1st lines differ - expected: '4', found: '7'