您的位置:首页 > 其它

任务

2015-07-26 09:44 246 查看
zoj Tree of Tree

/*Author :usedrose  */
/*Created Time :2015/7/26 1:06:37*/
/*File Name :2.cpp*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <vector>
#include <string>
#include <ctime>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
#define MAXN 110
#define OK cout << "ok" << endl;
#define o(a) cout << #a << " = " << a << endl
#define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
using namespace std;
typedef long long LL;

int n, sz;
vector<int> G[MAXN];
int dp[MAXN][MAXN];

void dfs(int fa, int u)
{
for (int i = 0;i < G[u].size(); ++ i) {
int v = G[u][i];
if (v == fa) continue;
dfs(u, v);
for (int k = sz;k >= 1;-- k)
for (int cur = 1;cur <= k; ++ cur)
dp[u][k] = max(dp[u][cur], dp[u][cur] + dp[v][k - cur]);
}
}

int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> n >> sz) {
memset(dp, 0, sizeof(dp));
for (int i = 0;i <= n; ++ i) {
G[i].clear();
}
for (int i = 0;i < n; ++ i)
cin >> dp[i][1];
int x, y;
for (int i = 0;i < n - 1; ++ i) {
cin >> x  >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(-1, 0);
int ans = 0;
for (int i = 0;i < n; ++ i)
ans = max(ans, dp[i][sz]);
cout << ans << endl;

}
return 0;
}


View Code
hdu5285

hdu 3989

BestCoder anniversary

poj 3057 http://www.cnblogs.com/naturepengchen/articles/4179299.html
zoj 月赛

挑战编程竞赛

KM算法 http://www.cnblogs.com/skyming/archive/2012/02/18/2356919.html
2道 二分图题集的题目

hdu 5229

[title]Problem B. ZCC loves strings[/title] 结论:对于串a和b,游戏中先手必胜当且仅当∣a∣+∣b∣|a|+|b|∣a∣+∣b∣为奇数或a=ba=ba=b. 我们按∣a∣+∣b∣|a|+|b|∣a∣+∣b∣的大小从小到大考虑所有的情况。 当∣a∣+∣b∣=0|a|+|b|=0∣a∣+∣b∣=0时,显然先手必败,符合结论。 假设已经证明了∣a∣+∣b∣=k(k<p)|a|+|b|=k(k<p)∣a∣+∣b∣=k(k<p)的所有情况满足结论,现在考虑∣a∣+∣b∣=p|a|+|b|=p∣a∣+∣b∣=p的情况。 若ppp是奇数,先手只需要选择长度较短的不为空的串,并使用A操作,就可以转移到∣a∣+∣b∣|a|+|b|∣a∣+∣b∣为偶数并且两个串不相等或者两个串均为空的情况,这种情况先手必败,故此时先手必胜。 若ppp是偶数,如果两个串相等,显然先手只需要选择使用B操作就能获得胜利了。否则,无论先手如何操作,都只能转移到∣a∣+∣b∣|a|+|b|∣a∣+∣b∣为奇数的先手必胜的情况。故此时先手必败。 因此,按顺序考虑每一个串,求得在其之前出现的串中,长度奇偶性与其不同的串共有x个,与其完全相同的串有y个,则对答案有x+yx+yx+y的贡献。累加即可。

hdu 5225

[title]Problem B[/title] 从1到n枚举k,表示当前要计算的排列与读入的排列前k-1项相同,而第k项不同。对于每一个k,再枚举一个t,表示当前要计算的排列的第k项是t,所以t要比读入的排列的第k项小,并且不与前k-1个数中的任意一个数相等。 那么,剩下的n-k个数任意排列,都满足字典序小于读入的排列。所以要计算它们的逆序对数之和。可以分情况计算: 1、逆序对中的两个数都在前k-1个位置,可以对于每一个k都暴力计算。 2、逆序对中的一个数在前k-1个位置,另一个数不在,同样可以对于每一个k都暴力计算。 3、逆序对中的一个数在第k个位置,另一个数在后n-k个位置。也可以暴力计算。 4、逆序对中的两个数都在后n-k个位置。这个值可以DP预处理,也可以推出一个式子直接计算。 可以这样考虑:在后n-k个位置中,有一半的排列方式中,第i小的数在第j小的数(i>j)的前面。共有(n-k)!种排列方式,所以对于一对数,有(n−k)!2\frac{(n-k)!}{2}
http://www.cnblogs.com/usedrosee/p/4714952.html
​2​​(n−k)!​​种排列方式中是逆序对。共$\frac{(n-k)·(n-k-1)}{2}$对数,所以这类逆序对共$\frac{(n-k)·(n-k-1)·(n-k)!}{4}$对。 时间复杂度:O(n3{n}^{3}n​3​​)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: