题目链接: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. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  2. Crontab 执行时没有环境变量!

    Crontab 执行时没有环境变量! Crontab 执行时没有环境变量! Crontab 执行时没有环境变量! 重要的事情说三遍,浪费我半天时间去找问题!! 非系统默认工具,执行时候需要加全路径!!

  3. react源码第一天

    1.下载源码:github 16.7版本 2.找到笔记:https://react.jokcy.me/book/api/react.html#

  4. delphi frame 添加 create onshow 事件

    delphi frame  添加 create onshow 事件 https://www.cnblogs.com/Gufan/archive/2011/09/06/2538932.html proc ...

  5. redis伪集群脚本

    #安装redis伪集群脚本,先把redis-..gem及启动脚本放在/data1/redis-cluster目录下,然后执行该脚本即可 #!/bin/bash set -e #获取redis本机ip ...

  6. UE4 C++ 笔记

    1.获取当前的GameMode:GameMode = Cast<你GameMode的类名>( UGameplayStatics::GetGameMode(this)); 2.输出日志Deb ...

  7. sql两表连接

    一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表大小相当,那么用in和exists差别不大.如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:例 ...

  8. 史上最全最详细的环境搭建教程,行百里者手把手教你在windows下搭建Anaconda+pycharm+库文件(TensorFlow,numpy)环境搭建

    我是在搭建TensorFlow开发环境的道路上走了很多弯路 掉了很多头发,为了让广大同学们不在受苦受累 下面我将手把手教你学习如特快速搭建python环境 快速导入numpy,PIL,pillow,等 ...

  9. kvm动态添加硬盘

    1.创建硬盘. qemu-img create -f qcow2 /data/data_root/vm-images/xxxx.qcow2 20G 2.添加硬盘 方式1.动态添加: virsh att ...

  10. [Solution] 969. Pancake Sorting

    Difficulty: Medium Problem Given an array A, we can perform a pancake flip: We choose some positive ...