QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#278575 | #5455. TreeScript | chitoge# | WA | 26ms | 3876kb | C++23 | 2.0kb | 2023-12-07 17:31:03 | 2023-12-07 17:31:04 |
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);
int cnt = 0;
for(auto it : G[now]) {
if(it == fa) continue;
if(buff[it]) ++cnt;
}
for(auto it : G[now]) {
if(it == fa) continue;
//cerr << it << ' ' << fa << ' ' << ans << endl;
if(cnt >= 2) {
dfs(it , now , m + 1);
}
else {
if(cnt == 0) {
ans = max(ans , m + 1);
return;
}
if(buff[it]) dfs(it , now , m);
else dfs(it , now , m + 1);
}
}
};
pre(1 , -1);
bool ok = true;
for(int j = 2 ; j < n ; ++j) {
if(buff[j] == true) {
ok = false;
break;
}
}
if(ok) {
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: 0ms
memory: 3876kb
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: 3744kb
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:
7 7 4 7 6 6 9 9 5 6 6 9 11 7 5 5 7 9 5 9 9 4 7 7 8 6 11 5 6 8 1 12 7 4 9 7 4 5 5 7 8 5 7 4 6 7 8 10 6 6 5 3 7 8 10 8 6 8 6 8 10 9 4 8 8 5 5 7 10 6 8 8 10 6 8 11 3 5 7 1 1 8 10 6 5 5 7 11 9 9 9 9 6 8 5 5 10 5 7 8 9 9 9 9 8 9 4 4 10 5 7 7 5 4 7 7 4 3 6 8 4 8 3 10 6 7 7 5 9 7 14 6 8 5 7 3 11 6 6 4 4 6 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '7'