QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#129272#5455. TreeScripttselmegkh#WA 8ms8324kbC++201.3kb2023-07-22 13:24:192023-07-22 13:24:21

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-22 13:24:21]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:8324kb
  • [2023-07-22 13:24:19]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;

const int N = 2e5 + 5, inf = 1e9;
#define pb push_back
#define mp make_pair
#define ll long long
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define sz(x) (int)x.size()
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;

vi g[N];
int ans;

void dfs(int u, int p){
    int cnt = 0;
    for(int v : g[u]){
        if(v == p)continue;
        cnt += (sz(g[v]) > 1);
    }
    if(cnt >= ans && sz(g[u]) > 2){
        ans += (cnt - (ans - 1));
    }
    for(int v : g[u]){
        if(v == p)continue;
        dfs(v, u);
    }
}
void solve(){
	int n;
	cin >> n;
	
    int root = -1;
    for(int i = 0; i <= n; i++){
        g[i].clear();
    }
	for(int i = 1; i <= n; i++){
		int x; cin >> x;
        g[i].pb(x);
        g[x].pb(i);
        if(x == 0)root = i;
	}
    ans = 1;
    dfs(root, 0);
    cout << ans << '\n';
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t = 1;
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 8324kb

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: 8ms
memory: 8212kb

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
5
4
11
6
7
8
10
4
6
5
7
9
9
5
6
8
7
6
8
8
4
6
7
6
8
9
5
6
11
4
8
4
5
9
6
4
5
4
6
4
8
7
5
6
5
10
8
6
6
8
4
5
6
9
8
8
8
5
10
7
6
4
9
6
7
5
7
9
8
5
8
9
6
6
10
4
4
6
1
1
9
10
6
5
7
10
9
6
7
7
7
6
7
6
8
5
5
9
10
6
6
8
9
8
10
4
3
8
6
7
7
5
5
8
6
6
3
8
6
3
10
6
12
4
6
6
4
10
8
11
6
6
6
6
5
9
6
5
4
4
4
4
...

result:

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