QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#777575 | #9751. 覆盖一棵树 | ERin_cy# | WA | 36ms | 5588kb | C++20 | 1.3kb | 2024-11-24 03:46:25 | 2024-11-24 03:46:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int fa[N];
vector<int>e[N];
int dep[N];
void dfs(int r,int f){
dep[r] = dep[f] + 1;
if(e[r].size() == 1 && r != 1){
return;
}
for(int i = 0;i<e[r].size();i++){
if(e[r][i] == f) continue;
dfs(e[r][i],r);
}
}
bool cmp(int a,int b){
return dep[a] < dep[b];
}
void solve(){
set<pair<int,int>>st;
int n;
cin >> n;
for(int i = 1;i<n;i++){
cin >> fa[i + 1];
e[i + 1].push_back(fa[i]);
e[fa[i]].push_back(i + 1);
}
dfs(1,0);
vector<int>leaf;
for(int i = 2;i<=n;i++){
if(e[i].size() == 1){
leaf.push_back(i);
}
}
sort(leaf.begin(),leaf.end(),cmp);
int ans = 0;
for(auto x : leaf){
int te = x;
int cnt = 0;
while(te != 1 && st.find(make_pair(te,fa[te])) == st.end()){
st.insert(make_pair(te,fa[te]));
te = fa[te];
cnt++;
}
ans = max(ans,cnt);
}
cout << ans << endl;
for(int i = 1;i<=n;i++){
e[i].clear();
dep[i] = 0;
}
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5588kb
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: 36ms
memory: 3660kb
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:
6 8 7 5 3 1 1 7 1 1 9 1 1 9 1 2 7 3 1 3 1 6 7 5 6 3 1 1 9 6 3 3 4 5 7 3 3 7 4 4 9 3 3 3 7 1 7 5 1 3 5 3 2 1 5 2 4 2 3 5 1 1 4 1 1 6 3 9 1 5 4 3 1 1 1 2 1 9 1 5 1 6 3 4 1 9 6 1 3 1 3 3 1 1 7 3 7 3 7 6 6 5 4 6 7 4 4 2 4 5 1 7 6 5 1 1 2 2 7 3 3 5 3 2 3 3 1 1 3 7 5 5 7 5 4 1 3 7 5 1 3 1 7 7 9 1 1 7 4 1 ...
result:
wrong answer 1st lines differ - expected: '4', found: '6'