Submission #550301

# Submission time Handle Problem Language Result Execution time Memory
550301 2022-04-17T20:15:36 Z aryan12 Cat in a tree (BOI17_catinatree) C++17
51 / 100
1000 ms 22060 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 2e5 + 5;
vector<int> g[N];
int n, d;

deque<int> dfs(int node, int par)
{
	deque<int> cur_ans;
	cur_ans.push_back(1); // this node
	for(int to: g[node])
	{
		if(to == par)
		{
			continue;
		}
		deque<int> child_ans = dfs(to, node);
		child_ans.push_front(child_ans[0]); // maintaining depths
		if(child_ans.size() > cur_ans.size())
		{
			swap(child_ans, cur_ans);
		}
		for(int i = 0; i < child_ans.size(); i++)
		{
			int pos = d - i;
			int max_val = child_ans[i] + ((pos < cur_ans.size()) ? (cur_ans[max(i, pos)]) : (0LL));
			max_val = max(max_val, cur_ans[i] + ((pos < child_ans.size()) ? (child_ans[max(i, pos)]) : (0LL)));
			// cout << "node = " << node << ", to = " << to << endl;

			// max_val = ((pos < cur_ans.size()) ? (child_ans[i] + cur_ans[pos]) : (0));
			// cout << "node = " << node << ", to = " << to << endl;
			// max_val = max(max_val, ((pos < child_ans.size()) ? (child_ans[pos] + cur_ans[i]) : (0)));
			// cout << "node = " << node << ", to = " << to << endl;
			cur_ans[i] = max_val;
		}
		// cout << "cur_ans.size() = " << cur_ans.size() << "\n";
		for(int i = cur_ans.size() - 2; i >= 0; i--)
		{
			cur_ans[i] = max(cur_ans[i], cur_ans[i + 1]);
		}
	}
	return cur_ans;
}

void Solve() 
{
	cin >> n >> d;
	for(int i = 1; i < n; i++)
	{
		int x;
		cin >> x;
		g[i].push_back(x);
		g[x].push_back(i);
	}
	cout << dfs(0, -1)[0] << "\n";
}

int32_t main() 
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) 
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}

Compilation message

catinatree.cpp: In function 'std::deque<long long int> dfs(long long int, long long int)':
catinatree.cpp:27:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |   for(int i = 0; i < child_ans.size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~~
catinatree.cpp:30:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |    int max_val = child_ans[i] + ((pos < cur_ans.size()) ? (cur_ans[max(i, pos)]) : (0LL));
      |                                   ~~~~^~~~~~~~~~~~~~~~
catinatree.cpp:31:46: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |    max_val = max(max_val, cur_ans[i] + ((pos < child_ans.size()) ? (child_ans[max(i, pos)]) : (0LL)));
      |                                          ~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 3 ms 5024 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 3 ms 5024 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 12 ms 6292 KB Output is correct
22 Correct 4 ms 5016 KB Output is correct
23 Correct 4 ms 5020 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 5 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 4 ms 5020 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5064 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 5 ms 5332 KB Output is correct
39 Correct 6 ms 5460 KB Output is correct
40 Correct 11 ms 6228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 3 ms 5024 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 12 ms 6292 KB Output is correct
22 Correct 4 ms 5016 KB Output is correct
23 Correct 4 ms 5020 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 5 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 4 ms 5020 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5064 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 5 ms 5332 KB Output is correct
39 Correct 6 ms 5460 KB Output is correct
40 Correct 11 ms 6228 KB Output is correct
41 Correct 71 ms 12680 KB Output is correct
42 Correct 47 ms 9272 KB Output is correct
43 Correct 60 ms 9332 KB Output is correct
44 Correct 44 ms 9320 KB Output is correct
45 Correct 36 ms 9224 KB Output is correct
46 Correct 105 ms 13692 KB Output is correct
47 Correct 121 ms 13668 KB Output is correct
48 Correct 115 ms 13636 KB Output is correct
49 Correct 98 ms 13636 KB Output is correct
50 Correct 33 ms 9556 KB Output is correct
51 Correct 38 ms 9532 KB Output is correct
52 Correct 44 ms 9576 KB Output is correct
53 Correct 80 ms 14112 KB Output is correct
54 Correct 81 ms 14112 KB Output is correct
55 Correct 89 ms 14112 KB Output is correct
56 Correct 12 ms 5844 KB Output is correct
57 Execution timed out 1084 ms 22060 KB Time limit exceeded
58 Halted 0 ms 0 KB -