题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228

Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1693    Accepted Submission(s): 978

Problem Description
Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem.
Now we decide to colour its nodes with k distinct colours, labelled from 1 to k. Then for each colour i = 1, 2, · · · , k, define Ei as the minimum subset of edges connecting all nodes coloured by i. If there is no node of the tree coloured by a specified colour i, Ei will be empty.
Try to decide a colour scheme to maximize the size of E1 ∩ E2 · · · ∩ Ek, and output its size.
Input
The first line of input contains an integer T (1 ≤ T ≤ 1000), indicating the total number of test cases.
For each case, the first line contains two positive integers n which is the size of the tree and k (k ≤ 500) which is the number of colours. Each of the following n - 1 lines contains two integers x and y describing an edge between them. We are sure that the given graph is a tree.
The summation of n in input is smaller than or equal to 200000.
Output
For each test case, output the maximum size of E1 ∩ E2 ... ∩ Ek.
Sample Input
3 4 2 1 2 2 3 3 4 4 2 1 2 1 3 1 4 6 3 1 2 2 3 3 4 3 5 6 2
Sample Output
1 0 1
给你n个节点,k个颜色,要你用k个颜色去涂这n个节点。Ei表示将所有颜色为i的结点连起来的最小边数。E1 ∩ E2 ... ∩ Ek表示E1 E2...Ek的重合边数,输出最大的E1 ∩ E2 ... ∩ Ek。
求出每个节点的子树大小(包括自己),如果子树大小大于等于k并且n-子树大小也大于等于k,ans+1。
#include<iostream>
#include<vector>
using namespace std;
#define maxn 300000
int n,k,cnt,ans,size[maxn],head[maxn];
struct edge{int to,next;
}e[maxn];
vector<int>ve[maxn];
void add(int u,int v)
{e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
}
void dfs(int u,int f)
{for(int i=0;i<ve[u].size();i++){int x=ve[u][i];if(x==f)continue;dfs(x,u);size[u]+=size[x];}if(size[u]>=k&&n-size[u]>=k)ans++;
}
int main()
{int t;cin>>t;while(t--){cin>>n>>k;int u,v;for(int i=1;i<=n;i++){ve[i].clear();size[i]=1;}for(int i=1;i<n;i++){cin>>u>>v;add(u,v);ve[u].push_back(v);ve[v].push_back(u);}ans=0;dfs(1,0);cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/chen99/p/10706615.html

2017沈阳站 Tree相关推荐

  1. 2017沈阳站流水账+感想

    第一场区域赛,在东北大学,刚开始找错旅馆,吓了一大跳, 以为是这一个,(心里想,妈耶学校这么缺钱?),后来发现这里是以前的,现在搬位置了...东北大学还是挺大的 热身赛:一上来看见对面是西交,还说了一 ...

  2. 极客大挑战2020_五省同燃,千人开赛。2020年全国轮滑大联动暨MX名星轮滑伴我行城市轮滑挑战赛——扬州站、襄阳站、南宁站、潍坊站、沈阳站完美收官...

    新力量,星未来.强健体魄,有氧健康,阳光轮滑,赛事先行.2020年全国轮滑大联动暨MX名星轮滑伴我行城市挑战赛全国巡回持续进行中.先后于8月26日收官江苏扬州站.8月27日收官山东潍坊站.8月30日收 ...

  3. Build Tour 2017 中国站北京、上海报名了

    微软于 5 月 10 日在总部西雅图举办的 Build 2017 大会上,发布了针对云计算.人工智能.Windows 以及混合现实平台等技术的一系列重要更新,这令众多来自企业.ISV.初创企业的开发者 ...

  4. HDU6218 2017ACM/ICPC亚洲区沈阳站 Bridge(Set,线段树)

    HDU6218 2017ACM/ICPC亚洲区沈阳站 Bridge Solution 我们考虑维护在环上的边的个数,答案就是总边数减去环上边数. 环的形态是这样的:(0,l),(0,l+1)...(0 ...

  5. 2016ICPC沈阳站

    2016ICPC沈阳站 题号 题目 知识点 难度 A Thickest Burger 贪心 签到 B Relative atomic mass 贪心 签到 C Recursive sequence 矩 ...

  6. 技术人的充电时刻,200分钟QA交流,尽在SDCC 2017·深圳站

    在互联网大潮下,2017年的就业环境越发恶劣,技术人如何去适应技术变革和学习新技术,以及如何快速增强自身的技术实力成为亟需解决的问题.如果你在看完一本技术图书后还是疑惑不解,处于目前项目遇到困难时无人 ...

  7. SDCC 2017·深圳站八大不容错过的理由

    在互联网大潮下,2017年的就业环境越发恶劣,技术人如何去适应技术变革和学习新技术,以及如何快速增强自身的技术实力成为亟需解决的问题.如果你在看完一本技术图书后还是疑惑不解,处于目前项目遇到困难时无人 ...

  8. 2018 ICPC 沈阳站

    细胞色素训练3 排名:100/193 2018年ICPC沈阳站,学长在这里拿金了.听学长说开始时候很快的出了两题,排名第四,是可以进final的,然后一直没过题,直到最后封榜时候连过两题,金牌最后一名 ...

  9. 2021icpc亚洲赛区沈阳站总结

    2021icpc亚洲赛区沈阳站总结&&赛题复盘(打铁记) 个人感想 大学生涯的第一场acm,我也可能跟很多人一样吧,没打的时候踌躇满志,打完抑郁不堪.也很有幸,能跟许许多多耳熟能详的老 ...

最新文章

  1. 如何取得sql语句的运行时间
  2. JavaScript中的面向对象程序设计
  3. matplotlib显示中文
  4. 应该知道的Linux技巧
  5. jQuery Event.stopPropagation() 函数详解
  6. Jenkins中构建时提示:Couldn‘t find any revision to build. Verify the repository and branch config
  7. XCode 4.2(4.1)真机调试及生成IPA全攻略
  8. MySQL where后面的行子查询使用
  9. dotTrace 6.1帮你理解SQL查询如何影响应用性能
  10. 进入51cto之后的发展方向
  11. 此模块的调试信息中缺少源信息_22python中的模块
  12. Linux scp 使用详解
  13. ZOJ4037 Peer Review
  14. 'gbk' codec can't encode character解决方法
  15. 使用Sqlloader处理数据
  16. NOIP2013华容道
  17. 使用 webSocket 连接菜鸟打印(并获取当前电脑连接的打印机信息)
  18. vba中find用法
  19. 解决Adobe Acobat设置了背景色,显示出现白条的问题!
  20. 学术论文如何撰写数据分析部分?

热门文章

  1. 人大计算机在职考研好考吗,人大在职研究生好考吗?通过率高吗?
  2. java 反射 内存_Java内存到反射入门
  3. JavaScript——使用对话框
  4. 域名服务器的配置文档,dns域名服务器的配置
  5. OpenCV_08 边缘检测:Sobel检测算子+Laplacian算子+Canny边缘检测
  6. excel记账本模板_原来这才是老板最喜欢看的财务报表!这些模板送你,录入自动生成...
  7. Joi验证模块的使用
  8. LeetCode 1839. 所有元音按顺序排布的最长子字符串(滑动窗口)
  9. LeetCode 107. 二叉树的层次遍历 II(队列)
  10. LeetCode 221. 最大正方形(DP)