题目链接: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. ssm注入失败

    今天做ssm整合时候,创建bean/注入一直出错,检查几遍没发现问题,后来发现犯了个低级错误,mapper.xml的<mapper namespace="XXXXX" > ...

  2. 分布式之redis核心知识盘点?

    考虑到绝大部分写业务的程序员,在实际开发中使用redis的时候,只会setvalue和getvalue两个操作,对redis整体缺乏一个认知.又恰逢博主某个同事下周要去培训redis,所以博主斗胆以r ...

  3. Matplotlib-画图种类

    Scatter 散点图 本节我们将讲述各种不同的plot的方式.之前我们讲到了如何plot线,今天我们讲述如何plot散点图. # 首先,先引入matplotlib.pyplot简写作plt,再引入模 ...

  4. node读取文件转换json文件

    { ".323":"text/h323" , ".3gp":"video/3gpp" , ".aab" ...

  5. css样式的书写顺序及原理——很重要!

    记得刚开始学习前端的时候,每次写css样式都是用到什么就在样式表后添加什么,完全没有考虑到样式属性的书写顺序对网页加载代码的影响.后来逐渐才知道正确的样式顺序不仅易于查看,并且也属于css样式优化的一 ...

  6. webservice 教程

    https://ke.qq.com/webcourse/index.html#cid=28875&term_id=100182700&taid=800324205965515& ...

  7. LINUX系统日常使用命令

    一.开关机命令1.shutdown -h now   关闭系统2.init 0   关闭系统3.telinit 0  关闭系统4.shutdown -h hours:minutes      按预定时 ...

  8. Taro开发写密码支付弹层

    在支付的时候弹出填写密码,模仿了支付宝支付填写密码.主要是利用遮罩的来实现.直接上代码吧. html设计,通过标记控制显示. { showPayPwdInput ? <View classNam ...

  9. hibernate的开始

    1.1对象的持久化 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术.(hibernate是通过id来管理对象) 1.2怎样实现持久化 1 对象序列化 2 JDBC 3 O ...

  10. C++/CLI

    [C++/CLI] A C++/CLI application or component uses extensions to C++ syntax (as allowed by the C++ Sp ...