QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#278991 | #5455. TreeScript | chitoge | WA | 26ms | 3920kb | C++14 | 1.6kb | 2023-12-08 00:36:01 | 2023-12-08 00:36:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<int> a(n);
vector<vector<int> > G(n + 1);
for(auto &it : a) cin >> it;
if(n == 2) {
cout << 1 << '\n';
return;
}
for(int i = 1 ; i < n ; ++i) {
auto x = a[i] , y = i + 1;
G[y].emplace_back(x);
G[x].emplace_back(y);
}
vector<bool> buff(n + 1 , false);
function<void(int , int)> pre = [&](int now , int fa) {
if(G[now].size() >= 3 && now != 1) buff[now] = true;
if(G[now].size() >= 2 && now == 1) buff[now] = true;
//cerr <<"w"<< now << ' ' << buff[now] << endl;
for(auto it : G[now]) {
if(it == fa) continue;
pre(it , now);
if(buff[it]) buff[now] = true;
}
};
int ans = 0;
function<void(int , int , int)> dfs = [&](int now , int fa , int m) {
ans = max(ans , m);
//cerr << now << ' ' << ans << ' ' << m << endl;
int cnt = 0;
for(auto it : G[now]) {
if(it == fa) continue;
if(buff[it]) ++cnt;
}
if(cnt == 0) {
//cerr <<"w"<< now << ' '<<ans << ' ' << m << endl;
ans = max(ans , m + 1);
return;
}
for(auto it : G[now]) {
if(it == fa) continue;
//cerr << it << ' ' << fa << ' ' << m << endl;
if(cnt >= 2) {
dfs(it , now , m + 1);
}
else {
if(buff[it]) dfs(it , now , m);
else ans = max(ans , m + 1);
}
}
};
pre(1 , -1);
if(!buff[1]) {
cout << 1 << '\n';
return;
}
dfs(1 , -1 , 1);
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3816kb
input:
2 3 0 1 2 7 0 1 2 2 1 4 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: -100
Wrong Answer
time: 26ms
memory: 3920kb
input:
1000 197 0 1 1 2 1 4 1 5 8 3 5 1 4 7 12 14 4 7 10 9 12 11 16 10 21 19 22 17 25 13 28 9 5 15 26 26 33 25 15 1 35 6 32 17 37 8 19 43 19 27 29 9 30 6 31 27 35 35 37 13 28 38 57 31 38 8 22 14 33 9 18 62 52 37 10 19 22 60 54 12 38 59 64 65 80 82 28 60 85 78 27 25 71 14 52 6 59 14 87 32 33 41 59 41 88 38 ...
output:
6 7 3 6 5 6 8 8 5 5 6 8 10 7 4 5 7 9 4 8 9 3 6 7 7 5 10 4 6 8 2 12 6 4 9 6 4 4 4 7 7 4 6 3 5 7 8 9 5 5 4 2 7 7 10 8 5 7 5 7 9 8 4 8 8 5 4 7 10 6 7 8 10 5 8 10 2 4 7 1 1 8 9 6 5 5 7 10 9 8 8 9 6 7 5 5 10 4 6 7 8 9 8 8 8 8 3 3 10 5 7 6 4 4 6 6 3 2 5 8 4 7 3 9 5 6 7 5 8 7 13 6 8 4 7 3 11 6 6 4 3 6 4 2 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '6'