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 ;
}

  

2017 ACM/ICPC 沈阳 L题 Tree的更多相关文章

  1. 2017 ACM/ICPC 沈阳 K题 Rabbits

    Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number line, each occupying a ...

  2. 2017 ACM/ICPC 沈阳 I题 Little Boxes

    Little boxes on the hillside. Little boxes made of ticky-tacky. Little boxes. Little boxes. Little b ...

  3. 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path

    The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and s ...

  4. 2017 ACM/ICPC 沈阳 F题 Heron and his triangle

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

  5. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  6. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

  7. 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest

    2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...

  8. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  9. 2017 ACM/ICPC Shenyang Online SPFA+无向图最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

随机推荐

  1. 启动Spring Tool Suite 4时出现 could not find tools.jar spring boot live hovers....弹窗

    第一步:检查一下STS启动时的加载环境 Help  —› About Spring Tool Suite 4 —› Installation Details —› Configuration 本人已经 ...

  2. nyoj 53-不高兴的小明 (遍历)

    53-不高兴的小明 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:28 submit:89 题目描述:    小明又出问题了.妈妈认为聪明的小明应该 ...

  3. gopls替换hover文档

    一直在用vscode来开发golang程序,也一直在用 gopls语言服务器,也一直用鼠标悬浮显示函数的文档. 今天 偶然关闭了 gopls,然后 鼠标悬浮后,发现了 新大陆,nima,gopls的 ...

  4. vuejs 入门

    配置环境教程网上太多了,就不赘述了,建议使用npm 命令行安装,总觉得这个念头不会使用命令行配置环境的就不是一个好的程序员,或者为了方便也可以直接在本地建一个.html文件,然后引用下面的链接即可. ...

  5. go语言学习笔记(二)

    整数 有符号整数 int8 int16 int32 int64 无符号整数 uin8 uin16 uin32 uin64 无符号整数 uintptr可以进行运算这点很重要请了解unsafe包,大小不明 ...

  6. Ubuntu 16.04源码编译boost库 编写CMakeLists.txt | compile boost 1.66.0 from source on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/d5d4a460/,欢迎阅读! compile boost 1.66.0 from source on ubuntu 16.04 G ...

  7. Spring与Shiro整合

    Spring整合篇--Shiro 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 什么是Shiro? 链接:https://www.cnblogs.com/StanleyBlogs/ ...

  8. python3之递归实例

    一.利用递归求: 1+2+3+4+5...+n的前n项和 def recursion_sum_1(n): #当n = 1:和为1 #否则,n的和等同于 n + (n -1) if n == 1: re ...

  9. sign in with apple后端校验(java)

    最近新开发的ios平台的app在提审的时候,被拒了,原因是app上如果有接第三方登陆(比如,微信,微博,facebook等),那就必须要接apple id登陆,坑爹~苹果霸权啊!然而没办法,靠他吃饭, ...

  10. windows安装Pytorch报错:from torch._C import * ImportError: DLL load failed: 找不到指定的模块”解决方案

    问题描述 python环境下安装cpu版本pytorch,安装成功,但是导入出错. 报错如下 解决方法 参考博客,大家解决方法大概有:升级numpy.添加.dll文件到环境变量,均没有成功.本地pyt ...