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 ∩ 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
 
中文题意:给你一个数T,代表case的个数;
然后每个case,给两个数node(点的个数),k(颜色的个数),接下来node-1行,每行两个数,代表这两个数之间存在edge;
然后保证每次case都是一棵树,现在用这k种颜色为树的节点进行染色,Ei代表第i种颜色的所有节点之间相连的所有边的集合;
E[1],E[2],E[3]...E[node]中公共边数为最大数量;
 
感想:第一次做这道题时,有点懵,想了一阵之后,脑海中有一点思路,既然是要求边,那就从边进行研究,可是脑海中的那一点灵光总是转不住,然后就在网上搜了题解,结果题解跟自己的那点灵光很像,但还是有一点地方有点疑惑;最后的AC代码还是在询问队友之后了解到了一些知识点采写出,总的来说我还是太菜了!!!需要努力!
 
思路:对于某一个结点来说,如果它的左边(算上自身)能有k个结点的话,而它的右边也能有k个节点的话,那么这个结点右边的这条边就会在所有的边集中。
 
AC代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=200010;
vector<int >ve[maxn];
int plug[maxn];
int sum[maxn];
int number(int x){ //对于这里额使用的是一个数组来标记这个点是否访问过,其实也可以直接将上一个访问的点放进来进行判断就好,因为这个图也是一棵树
for(int i=0;i<ve[x].size();i++){
if(plug[ve[x][i]]) continue;
plug[ve[x][i]]=1;
sum[x]+=number(ve[x][i]);
}
return sum[x];
}
int main(){
int T,node,k,x,y;
cin>>T;
while(T--){
scanf("%d%d",&node,&k);
for(int i=0;i<=maxn+5;i++) ve[i].clear();
memset(plug,0,sizeof(plug));
for(int i=0;i<=node;i++) sum[i]=1;
for(int i=0;i<node-1;i++){
scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
}
plug[1]=1;
number(1);
/*for(int i=1;i<node;i++) printf("%d ",sum[i]);
printf("%d\n",sum[node]);*/
int ans=0;
for(int i=1;i<=node;i++){
if(sum[i]>=k&&node-sum[i]>=k) ans++;
}
printf("%d\n",ans);
}
}

hdu6228Tree的更多相关文章

随机推荐

  1. 哪吒票房超复联4,100行python代码抓取豆瓣短评,看看网友怎么说

    <哪吒之魔童降世>这部国产动画巅峰之作,上映快一个月时间,票房口碑双丰收. 迄今已有超一亿人次观看,票房达到42.39亿元,超过复联4,跻身中国票房纪录第三名,仅次于<战狼2> ...

  2. 初学css list-style属性

    网上很多css布局中会看到这样的一句:list-style:none: 那么list-style到底什么意思?中文即:列表样式:无: 其实它是一个简写属性,包含了所有列表属性,具体包含list-sty ...

  3. ASP.NET中Literal控件的使用方法(用于向网页中动态添加内容)

    原文:https://www.jb51.net/article/82855.htm 可以将 Literal 控件用作网页上其他内容的容器.Literal 控件最常用于向网页中动态添加内容.简单的讲,就 ...

  4. 在vsCode中用git命令合并分支

    提交修改代码到本地仓库 $ git commit -m "修改的东西的描述"切换到master主分支上 $ git checkout master拉取主分支上面的代码 $ git ...

  5. go中基本数据类型转换为string类型的方法

    代码 // 基本数据类型转换为string类型 package main import ( "fmt" "strconv" ) func main() { // ...

  6. 两台服务器共享文件Windows和Linux

    最近遇到一个问题,新装的两台服务器死活就是挂载不上samba服务器上的文件,在输入mount挂载命令的时候提示只读什么的,尝试了在挂载的时候用只读的方式去挂载也是不行,最终发现了,缺少安装cifs包, ...

  7. spl_autoload_register() 函数实现的自动加载

    和Python用module来区分代码块不同,PHP按照命名空间来区分,开始学PHP的时候一心认定了如果想用 use 关键字来导入(Python的习惯说法)一个类或者函数或者其他对象的话,必须先inc ...

  8. web--响应式导航菜单

    响应式导航菜单 代码如下 HTML代码: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  9. 线程中sleep和wait方法的区别

    sleep() 方法: 线程主动放弃CPU,使得线程在指定的时间内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态.典型地,sleep()被用在等待某个资源就绪的情形:测试发 ...

  10. Java中的Overload和Override有什么区别

    Overload和Override的区别 1.Overload 定义 Overload是重载的意思.它是指我们可以定义一些名称相同的方法,通过定义不同的输入参数来区分这些方法,然后在调用时,虚拟机就会 ...