QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#87059#5455. TreeScriptwyjjjWA 69ms8792kbC++141.1kb2023-03-11 16:37:422023-03-11 16:37:50

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-11 16:37:50]
  • 评测
  • 测评结果:WA
  • 用时:69ms
  • 内存:8792kb
  • [2023-03-11 16:37:42]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 2e5 + 10;

struct node {
	int id;
	int y;
};
vector<node>v[N];
int fa[N];
int n;
int ans;
bool cmp(node a, node b) {

	return a.y > b.y;
}


int dfs(int u,int cnt1,int cnt2) {
	if (v[fa[u]].size() == 0 && fa[u] != 0)cnt2--;
	cnt2++;
	if (cnt2 > cnt1)cnt1++;
	if (v[u].size() == 0)cnt2--;
	while (v[u].size()) {
		auto x = v[u][v[u].size() - 1].id;
		v[u].pop_back();
		cnt1=dfs(x,cnt1,cnt2);
	}
	return cnt1;
}
int main() {
	int t;
	cin >> t;
	while (t--) {
		memset(fa, 0, sizeof fa);

		ans = 0;
		int root = -1;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			int x;
			cin >> x;
			if (x == 0)root = i;

			else {
				fa[i] = x;
				v[x].push_back({ i,0 });
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < v[i].size(); j++) {
				v[i][j].y = v[v[i][j].id].size();
			}
		}
		for (int i = 1; i <= n; i++) {
			sort(v[i].begin(), v[i].end(), cmp);
		}

		cout << dfs(root,1,0) << endl;
	}
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8792kb

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: 69ms
memory: 8760kb

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:

5
5
3
5
3
4
5
7
4
4
5
6
7
6
4
4
6
7
3
6
7
3
5
5
6
5
6
3
4
5
2
7
5
3
5
5
4
3
3
5
6
4
5
3
4
5
6
5
4
5
4
3
5
5
6
5
4
5
5
4
6
5
3
4
5
5
4
5
7
4
4
5
6
4
5
6
2
3
4
1
1
7
6
4
4
5
7
7
5
5
5
5
5
5
4
4
5
4
5
4
6
5
5
6
6
6
3
3
5
5
4
4
3
3
5
4
3
2
4
4
3
6
3
7
3
4
4
4
7
4
6
4
6
4
4
3
6
4
4
3
4
3
3
2
6
6
4
2
5
8
...

result:

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