Submission #490793

#TimeUsernameProblemLanguageResultExecution timeMemory
490793ntabc05101Biochips (IZhO12_biochips)C++14
100 / 100
340 ms406452 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...