QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#87067#5455. TreeScriptwyjjjWA 88ms11616kbC++141.2kb2023-03-11 16:40:202023-03-11 16:45:13

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:45:13]
  • 评测
  • 测评结果:WA
  • 用时:88ms
  • 内存:11616kb
  • [2023-03-11 16:40:20]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 3e5 + 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 cnt1;//总共的寄存器
int cnt2;//需要的寄存器

void dfs(int u) {
	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();
		dfs(x);
	}

}
int main() {
	int t;
	cin >> t;
	while (t--) {
		memset(fa, 0, sizeof fa);
		cnt1 = 0;//总共的寄存器
		cnt2 = 0;//需要的寄存器
		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);
		}
		cnt1 = 1;
		cnt2 = 0;
		dfs(root);
		cout << cnt1 << endl;
	}
	return 0;
}


详细

Test #1:

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

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: 88ms
memory: 11616kb

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'