QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#75836#5455. TreeScriptXKErrorTL 1ms8484kbC++14590b2023-02-06 12:47:072023-02-06 12:47:10

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-06 12:47:10]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:8484kb
  • [2023-02-06 12:47:07]
  • 提交

answer

#include <bits/stdc++.h>

#define maxn 200005

using namespace std;

int T;
int n;
int f[maxn];
vector<int> g[maxn];

void dfs(int u, int fa) {
	int mx = 0, cm = -1;
	for (int v : g[u]) {
		dfs(v, u);
		if (f[v] >= mx) cm = mx, mx = f[v];
		else if (f[v] >= cm) cm = f[v];
	}
	f[u] = max(mx, cm + 1);
}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			int x;
			scanf("%d", &x);
			if (x) g[x].push_back(i);
		}
		dfs(1, 0);
		printf("%d\n", f[1]);
	}
	return 0;
}
/*
2
3
0 1 2
7
0 1 2 2 1 4 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8484kb

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
Time Limit Exceeded

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:


result: