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.

InputThe 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. 
OutputFor each test case, output the maximum size of E1 ∩ E1 ... ∩ 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
题解:考虑某条边,则只要两边的2个顶点都大于等于k,则连边时一定会经过这条边,ans++; 参看代码:
 #include<bits/stdc++.h>
using namespace std;
#define clr(a,b,n) memset((a),(b),sizeof(int)*n)
typedef long long ll;
const int maxn = 2e5+;
int n,k,ans,num[maxn];
vector<int> vec[maxn]; void dfs(int u,int fa)
{
num[u]=;
for(int i=;i<vec[u].size();i++)
{
int v=vec[u][i];
if(v==fa) continue;
dfs(v,u);
num[u]+=num[v];
if(num[v]>=k&&n-num[v]>=k) ans++;
}
} int main()
{
int T,u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
clr(num,,n+); ans=;
for(int i=;i<=n;i++) vec[i].clear();
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(,-);
//for(int i=1;i<=n;++i) cout<<num[i]<<endl;
printf("%d\n",ans);
}
return ;
}

  

最新文章

  1. Java开发web的几种开发模式
  2. [转]Eclipse SVN冲突解决
  3. viewPager 的可滑动 Title
  4. Linux:安装图形界面
  5. 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
  6. centos 6.5 安装 buildbot-slave 0.8.9
  7. iOS开发——网络Swift篇&amp;JSON与XML数据解析
  8. 【最大流,二分图匹配】【hdu2063】【过山车】
  9. Android setOnTouchListener识别滑动手势
  10. Mac编程的官方文档(类似MSDN)
  11. for clean
  12. 32bit程序在64bit操作系统下处理重定向细节(转自http://bbs.pediy.com/showthread.php?t=89054)
  13. weblogic11g 配置数据源
  14. HDU 2665 Kth number(划分树)
  15. (二)部署solr7.1.0到tomcat
  16. C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径
  17. 这可能是史上最全的css布局教程
  18. process(进程)
  19. Agile敏捷开发Planning Poker简介
  20. Apache poi简介及代码操作Excel

热门文章

  1. hash值生成表后缀(分表方案)
  2. c#属性1(Property)
  3. oracle基础(基本介绍)
  4. 插入排序的代码实现(C语言)
  5. java编程思想第四版第十三章字符串 总结
  6. webStorm中NodeJs 没有智能提示
  7. nyoj 1364-治安管理 (INT_MAX)
  8. nyoj 484-The Famous Clock
  9. 初识JVM内存模型
  10. PHP数组具有的特性有哪些