Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一下代码QwQ

Posted by CuriousCat at 2016-08-21 12:01:34 on Problem 1947
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#define MAXN (150 + 5)
using namespace std;

int total[MAXN], n, p, dp[MAXN][MAXN], root = 1;
vector<int> son[MAXN];
int search(int x) {
	total[x] = 1;
	for (size_t i = 0;i < son[x].size();++i)
		total[x] += search(son[x][i]);
	dp[x][1] = son[x].size();
	for (size_t i = 0;i < son[x].size();++i) {
		int s = son[x][i];
		for (int j = total[x];j >= 1;--j)
			for (int k = 1;k < j && k <= total[s];++k)
				dp[x][j] = min(dp[x][j], dp[x][j - k] + dp[s][k] - 1);
	}
	return total[x];
}

int main(int argc, char *argv[]) {
	cin >> n >> p;
	for (int i = 0;i < n - 1;++i) {
		int f, s;
		cin >> f >> s;
		son[f].push_back(s);
	}
	memset(dp, 0x7F, sizeof(dp));
	search(root);
	int ans = INT_MAX;
	for (int i = 1;i <= n;++i) 
		ans = min(ans, dp[i][p] + ((i == root) ? 0 : 1));
	cout << ans << endl;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator