QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#278711#5455. TreeScriptchitogeWA 26ms3652kbC++231.4kb2023-12-07 19:30:092023-12-07 19:30:10

Judging History

你现在查看的是最新测评结果

  • [2023-12-07 19:30:10]
  • 评测
  • 测评结果:WA
  • 用时:26ms
  • 内存:3652kb
  • [2023-12-07 19:30:09]
  • 提交

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) {
        if(G[now].size() >= 3 && now != 1) ++ans;
        if(G[now].size() >= 2 && now == 1) ++ans;
        for(auto it : G[now]) {
            if(it == fa) continue;
            dfs(it , now , m);
        }

    };
    pre(1 , -1);
    if(!buff[1]) {
        cout << 1 << '\n';
        return; 
    }
    dfs(1 , -1 , 1);
    cout << max(ans , 1) << '\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: 1ms
memory: 3436kb

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: 3652kb

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:

46
37
4
33
13
20
62
84
19
25
33
42
119
84
11
13
52
193
12
73
161
5
48
57
53
30
170
13
26
113
1
104
30
8
80
29
11
14
7
21
33
21
19
5
17
24
93
87
26
24
27
3
23
61
141
44
20
65
22
47
97
68
5
63
44
20
14
38
99
34
33
106
108
28
49
96
2
7
51
1
1
132
101
19
13
16
83
168
42
62
50
70
30
34
15
15
32
14
37
39
...

result:

wrong answer 1st numbers differ - expected: '4', found: '46'