QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#278996 | #5455. TreeScript | chitoge | WA | 26ms | 3760kb | C++14 | 1.6kb | 2023-12-08 00:48:18 | 2023-12-08 00:48:18 |
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);
}
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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3556kb
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: 3760kb
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:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '2'