Sicily 1140 国王的遗产

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

哈丁国的国王一生善于管理,勤于政务,在国家里聚积了大量的财富。但他众多的孩子都不争气,相互间时常勾心斗角,却没有一个真正能接受国王传位的人。为了避免将来某儿子一人独揽大权,又出于不能让权力过度分散的考虑,临终前,国王作了一个决定:

国王他将一生的财富打造出了一条很大的金块链,这条金块链的形状比较特别,它由n块大块的黄金组成,国王准备了n-1条链条,将某些相邻的两块大黄金用链条连接起来,最后构成一条连通的金块链。下图是国王构建的一条金块链:

国王对每块黄金编上号(从1到n),然后立下了遗嘱:

•   儿子们按照年龄大小顺序,在现存的金块链中获得遗产。

•   对于某个儿子,他可以在现存金块链中剪掉某条链条,获得不超过现有金块总数一半的那一部分。

•   某个儿子取得他那部分金块后,剩下的部分由他后面的弟弟们继续操作。

•   最后一个儿子获得剩下的那些金块,国王将保证每个儿子都能获得遗产。

儿子们都是贪婪的,他们都会选取使自己得到最多的金块的那条链条来剪,当然,有时选取的方案不是唯一的,但是儿子们都会选择使自己获得的“金块组编号”最小的那一条链来剪。“金块组编号”大小定义为:对于长度相同为L的两个有序数组A和B,A<B当且仅当存在一个整数i(0<i≤L),使得A[1]=B[1],…,A[i-1]=B[i-1]且A[i]<B[i]。

Input

输入数据第一行为一个整数n(1≤n≤30000)和一个整数k(1≤k≤100),分别表示金块的总数与国王儿子的数量。接下来n-1行,每行两个整数x和y,表示编号为x的金块与编号为y的金块用链条连接起来。

Output

输出数据只有一行,包含有k个整数,分别表示每个儿子获得金块的数量(由大儿子到最小的儿子)。

Sample Input

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

Sample Output

3 1 2

样例说明:这里,大儿子取得金块组{1,2,5},二儿子取得金块组{4},三儿子取得金块组{3,6}。

Problem Source

ZSUACM Team Member


Solution

给出一张图,每个人选择切一条边,取走较少的那一份金块。但是每个人都希望得到多的金块。

简单思考了一下,直接暴力穷举每一条边,然后再切分,总的搜索次数不超过30000*100,时间上可以承受。思路想到了,接下来就是怎么穷举并且切边的了。把图看成是无根树,选定根节点之后就可以重建树了。切分边的话,就可以看成是去掉树的一条边,将树分成子树和补图(即除子树之外的树状结构),然后比较即可。当然,选择子树节点越多越好,但是当节点一致时的编号问题就比较难弄了。

假设子图和子图比较,那么直接就是选择minNum最小的那一个,根据题目要求。因为在树状结构中,拥有同样的节点数和同样的minNum的话,就一定是同一个方案。

如果是补图和补图的比较的话,直接比较就有些困难,因为一般有同样的minNum,可以从补图所对应的子树的比较来比较补图的好坏,因为总的节点数是一样的,子树的minNum越小,说明该补图在实际比较中是越差的。

那么是补图和子图比较的话,直接比较minNum即可,如果每次建树的时候都选择最小的编号作为根的话,直接选择补图即可。

对于每一个方案,需要保存的信息有哪些呢?节点数是毋庸置疑的,minNum最小编号也是,该方案选择的是子树还是补图?方案的划分节点(即切除那条边)

于是乎,直接搜索就可以了。注意搜索结束之后需要染色标记取走的节点,最后一个儿子不需要搜索,剩下的就是他的。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 30005;
int res, minNum, startNode, total, rootID;
bool hasRoot;

struct Node
{
  vector<int> next;
  int tot, minID;
}node[maxn];

vector<int> u[maxn];
bool used[maxn];

void build(int root)
{
  used[root] = -1; // under used
  node[root].next.resize(0);
  node[root].tot = 1;
  node[root].minID = root;

  int len = u[root].size();
  for (int i = 0; i < len; ++i) if (used[u[root][i]] == 0)
  {
    int cur = u[root][i];
    build(cur);
    node[root].next.push_back(cur);
    node[root].tot += node[cur].tot;
    node[root].minID = min(node[root].minID, node[cur].minID);
  }
  used[root] = 0; // used end
}

void search(int root)
{
  vector<int> &next = node[root].next;
  int len = next.size();

  for (int i = 0; i < len; ++i)
  {
    int cur = next[i];
    int rest = total - node[cur].tot;
    if (node[cur].tot >= rest)
    {
      if (rest < res) continue;
      else if (rest > res || (rest == res && node[cur].minID > node[startNode].minID))
      {
        res = rest;
        hasRoot = true;
        minNum = rootID;
        startNode = cur;
      }
    }
    else
    {
      if (node[cur].tot < res) continue;
      else if (node[cur].tot > res || (!hasRoot && node[cur].minID < minNum))
      {
        res = node[cur].tot;
        hasRoot = false;
        minNum = node[cur].minID;
        startNode = cur;
      }
    }
    search(cur);
  }
}

void token(int root, int v)
{
  vector<int> &next = node[root].next;
  int len = next.size();

  for (int i = 0; i < len; ++i)
  {
    int cur = next[i];
    token(cur, v);
  }
  used[root] = v;
}

int main()
{
  int n, k, gone = 0;
  for (int i = 0; i < maxn; ++i) u[i].resize(0);
  memset(used, 0, sizeof(used));

  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; ++i)
  {
    int x, y;

    scanf("%d%d", &x, &y);
    u[x].push_back(y);
    u[y].push_back(x);
  }

  for (int i = 0; i < k-1; ++i)
  {
    int root = 1;

    while (used[root] && root < n) root++;
    build(root);
    // printf("root->%d:\n", root);
    // for (int j = 1; j <= n; ++j)
    // {
    //   printf("%d-next:", j);
    //   for (int k = 0; k < node[j].next.size(); ++k) printf(" %d", node[j].next[k]);
    //   printf("\n");
    //   printf("%d-tot: %d\n", j, node[j].tot);
    //   printf("%d-minID: %d\n", j, node[j].minID);
    // }
    // system("pause");
    res = 0;
    total = node[root].tot;
    rootID = root;
    search(root);
    // printf("%d-%d-%d-%d\n", res, minNum, startNode, hasRoot);
    if (hasRoot) token(root, 1), token(startNode, 0);
    else token(startNode, 1);

    printf("%d ", res);
    gone += res;
  }
  printf("%d\n", n - gone);

  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值