zoukankan      html  css  js  c++  java
  • [HDU5242] Game(树上贪心)

    【原题】

    Problem Description

    It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games simultaneously.

    One day he gets a new gal game named ''XX island''. There are n scenes in that game, and one scene will be transformed to different scenes by choosing different options while playing the game. All the scenes form a structure like a rooted tree such that the root is exactly the opening scene while leaves are all the ending scenes. Each scene has a value , and we use w**i as the value of the i-th scene. Once Katsuragi entering some new scene, he will get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he will get w**i for only once.

    For his outstanding ability in playing gal games, Katsuragi is able to play the game k times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for k times.

    Input

    The first line contains an integer T(T≤20), denoting the number of test cases.

    For each test case, the first line contains two numbers n,k(1≤kn≤100000), denoting the total number of scenes and the maximum times for Katsuragi to play the game ''XX island''.

    The second line contains n non-negative numbers, separated by space. The i-th number denotes the value of the i-th scene. It is guaranteed that all the values are less than or equal to 231−1.

    In the following n−1 lines, each line contains two integers a,b(1≤a,bn), implying we can transform from the a-th scene to the b-th scene.

    We assume the first scene(i.e., the scene with index one) to be the opening scene(i.e., the root of the tree).

    Output

    For each test case, output ''Case #t:'' to represent the t-th case, and then output the maximum total value Katsuragi will get.

    Sample Input

    2
    5 2
    4 3 2 1 1
    1 2
    1 5
    2 3
    2 4
    5 3
    4 3 2 1 1
    1 2
    1 5
    2 3
    2 4
    

    Sample Output

    Case #1: 10
    Case #2: 11
    

    【题意】

    选择k条叶子节点到根结点的树链,使得权值和最大 ,每个结点只能被统计一次。

    【思路】

    两次dfs,一次算出叶子结点的深度(叶子结点向根节点爬),一次计算权值和(已经被计算过则退出)。都要排序,取权值和最大的k个叶子结点。

    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <iomanip>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <unordered_map>
    #include <vector>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define endl '
    '
    #define lson  rt << 1
    #define rson  rt << 1 | 1
    #define f(x, y, z) for (int LL x = (y), __ = (z); x < __; ++x)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    using namespace std;
    
    const int maxn = 1e5 + 7;
    const int maxm = 1e9 + 7;
    const int mod = 1e9 + 7;
    LL n, k;
    LL w[maxn], ans[maxn], vis[maxn], f[maxn];
    
    struct node
    {
    	int id;
    	LL val;
    }v[maxn];
    bool cmp(node a, node b)
    {
    	return a.val > b.val;
    }
    
    void init()
    {
    	memset(v, 0, sizeof(v));
    	memset(vis, 0, sizeof(vis));
    	memset(f, 0, sizeof(f));
    }
    
    LL build(int u)
    {
    	if (!u) return 0;
    	if (v[u].val) return v[u].val;
    	v[u].val += w[u];
    	v[u].val += build(f[u]);
    	return v[u].val;
    }
    LL dfs(int u)
    {
    	if (vis[u]) return 0;
    	vis[u] = 1;
    	LL tmp = w[u];
    	if(f[u])tmp += dfs(f[u]);
    	return tmp;
    }
    
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	int t;
    	cin >> t;
    	int cas = 1;
    	while (t--)
    	{
    		cin >> n >> k;
    		init();
    		_rep(i, 1, n) cin >> w[i];
    		int ta, tb;
    		_rep(i, 1, n - 1)
    		{
    			cin >> ta >> tb;
    			f[tb] = ta;
    		}
    		_rep(i, 1, n)
    		{
    			v[i].id = i;
    			if (!v[i].val) v[i].val = build(i);
    		}
    		sort(v + 1, v + n + 1, cmp);
    		_rep(i, 1, n)
    		{
    			ans[v[i].id] = dfs(v[i].id);
    		}
    		sort(ans + 1, ans + 1 + n, greater<LL>());
    		LL res = 0;
    		_rep(i, 1, min(n, k)) res += ans[i];
    		cout << "Case #" << cas++ << ": " << res << endl;
    	}
    	
    }
    

    【类似题目】

    [gym102346-D:Denouncing Mafia][https://codeforces.com/gym/102346/problem/D]

  • 相关阅读:
    Flask第31课——include标签
    flask第30篇——宏macro和import标签
    HTML第三课——css盒子
    HTML第二课——css【2】
    HTML第二课——css
    HTML第一课——基础知识普及【2】
    转一篇数据库面试题
    自创建数字证书,安装到浏览器
    【转】Base64算法详解
    【转】二维码生成原理
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13498604.html
Copyright © 2011-2022 走看看