Submission #490793

# Submission time Handle Problem Language Result Execution time Memory
490793 2021-11-29T09:34:59 Z ntabc05101 Biochips (IZhO12_biochips) C++14
100 / 100
340 ms 406452 KB
#include<bits/stdc++.h>
using namespace std;

#define taskname ""

const int inf = 1e9;
const int mxN = 200001;
const int mxM = 501;

int n, m;
int a[mxN], prv[mxN];
vector<int> adj[mxN];
int dp[mxN][mxM];
vector<int> lst;

void dfs(int u, int p = -1) {
	prv[u] = lst.size();
	for (auto &to: adj[u]) {
		if (to != p) {
			dfs(to, u);
		}
	}
	lst.push_back(u);
}

int main() {
	if (fopen(taskname".inp", "r")) {
		freopen(taskname".inp", "r", stdin);
		freopen(taskname".out", "w", stdout);
	}
	else {
		if (fopen(taskname".in", "r")) {
			freopen(taskname".in", "r", stdin);
			freopen(taskname".out", "w", stdout);
		}
	}

	cin.tie(0)->sync_with_stdio(0);

	cin >> n >> m;
	int root = -1;
	for (int i = 0, x, y; i < n; i++) {
		cin >> x >> y; x--;
		a[i] = y;
		if (!(~x)) {
			root = i; continue;
		}
		adj[x].push_back(i);
		adj[i].push_back(x);
	}

	dfs(root);
	for (int j = 1; j <= m; j++) {
		dp[0][j] = -inf;
	}
	for (int i = 1; i <= lst.size(); i++) {
		auto &u = lst[i - 1];
		//cout << u << " " << prv[u] << "\n";
		for (int j = 1; j <= m; j++) {
			dp[i][j] = max(dp[i - 1][j], dp[prv[u]][j - 1] + a[u]);
		}
	}

	cout << dp[lst.size()][m] << "\n";

	return 0;
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:56:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |  for (int i = 1; i <= lst.size(); i++) {
      |                  ~~^~~~~~~~~~~~~
biochips.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:29:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |   freopen(taskname".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:33:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |    freopen(taskname".in", "r", stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:34:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |    freopen(taskname".out", "w", stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 2 ms 5196 KB Output is correct
4 Correct 12 ms 22820 KB Output is correct
5 Correct 14 ms 25164 KB Output is correct
6 Correct 14 ms 25164 KB Output is correct
7 Correct 224 ms 303028 KB Output is correct
8 Correct 224 ms 303156 KB Output is correct
9 Correct 285 ms 368256 KB Output is correct
10 Correct 340 ms 406452 KB Output is correct