题目链接: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=;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=;i<=n;i++)
{
ve[i].clear();
size[i]=;
}
for(int i=;i<n;i++)
{
cin>>u>>v;
add(u,v);
ve[u].push_back(v);
ve[v].push_back(u);
}
ans=;
dfs(,);
cout<<ans<<endl;
}
return ;
}
 

2017沈阳站 Tree的更多相关文章

  1. 2016ACM/ICPC亚洲区沈阳站-重现赛赛题

    今天做的沈阳站重现赛,自己还是太水,只做出两道签到题,另外两道看懂题意了,但是也没能做出来. 1. Thickest Burger Time Limit: 2000/1000 MS (Java/Oth ...

  2. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Thickest Burger Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  5. HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. 2015ACM/ICPC亚洲区沈阳站 Pagodas

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. 2015ACM/ICPC亚洲区沈阳站 B-Bazinga

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  8. HDU 6227.Rabbits-规律 (2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))

    Rabbits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  9. HDU 6225.Little Boxes-大数加法 (2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))

    整理代码... Little Boxes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/O ...

随机推荐

  1. QTP - 描述性编程

    描述性编程: 1.QTP的描述性编程能够摆脱测试对象库的限制,编写出更为复杂.适应能力更强的测试脚本. 2.即不需要在仓库晨定义,也能访问和操作实际对象. 3.用描述性编程编写的测试脚本在运行时,QT ...

  2. c博客作业01--顺序、分支结构

    1.本章学习总结 1.1思维导图 1.2本章的学习体会及代码量学习体会 1.2.1学习体会 本周学习的感受:原先基础是0的,所以本周学习起来并不是很轻松,有很多知识运用的 不是很熟,但是基本的知识是能 ...

  3. 打造适合自己的vim编辑器方法总结

    vim使用方法总结 说明:这是打造适合自己的vim编辑器的进阶方法,关于vim基础知识,请自行百度.也可参考文章末尾推荐blog网址 如果觉得自己打造vim编辑器麻烦,可以从github上面克隆一个, ...

  4. Java 8 默认方法

    转自:https://www.runoob.com/java/java8-default-methods.html Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不 ...

  5. lombok踩坑与思考

    虽然接触到lombok已经有很长时间,但是大量使用lombok以减少代码编写还是在新团队编写新代码维护老代码中遇到的. 我个人并不主张使用lombok,其带来的代价足以抵消其便利,但是由于团队编码风格 ...

  6. 在docker集群下,使用VNC,物理机器重启后VNC失败解决

    首先重装咯: sudo apt-get update sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon met ...

  7. CSS属性相关

    ·一.宽和高 width属性可以为元素设置宽度. height属性可以为元素设置高度. 块级标签才能设置宽度,内联标签的宽度由内容来决定. 二.字体属性 1.文字字体 font-family可以把多个 ...

  8. JAVA WEN开发环境与搭建

    一.下载安装JDK1.配置jdk开发环境JAVA_HOME 2.path 二.下载安装eclipse javaEE版本 三.安装部署tomcat3.1.安装: 直接解压到指定目录即可.(注:目录不要太 ...

  9. Win10系统下,本地连接远程桌面(Win2016)图解

    1.Win+R在运行框中输入mstsc,回车 2.在出现的远程登陆窗口中,计算机中输入远程桌面IP 3.点击显示选项按钮,在远程桌面窗口中,点击常规(默认),输入登陆的用户名: XXXXXX 4.点击 ...

  10. Tensorflow图像处理以及数据读取

    关于tensoflow的图像的处理,看到了一篇文章,个人觉得不错.https://blog.csdn.net/weiwei9363/article/details/79917942