hdu6228Tree
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.
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.
... ∩ Ek.
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
0
1
#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的更多相关文章
随机推荐
- 【洛谷p3956】棋盘
日常blog(✧◡✧) 棋盘[题目链接] 算法: 然后这是2017普及组: first.关于颜色处理:让c[i][j]=color+1:这样无色=0,红色=1,黄色=2: 然后其实是记忆化,将记答案的 ...
- dfs找环
http://acm.hdu.edu.cn/showproblem.php?pid=6736 Forest Program Time Limit: 2000/1000 MS (Java/Others) ...
- 8、前端知识点--关于Set用法的详解【ES6】
ES6提供了新的数据结构Set,它类似于数组,但是成员的值是唯一的,没有重复的值(对于基本类型来说).Set本身是一个构造函数,用来生成Set数据结构. 1.声明 let set = new Set( ...
- python Opencv图像基础操作
读取并显示图像 如果读取图像首先要导入OpenCV包,方法为: import cv2 读取并显示图像 img = cv2.imread("C:\test1.jpg") OpenCV ...
- Spark MLlib机器学习(一)——决策树
决策树模型,适用于分类.回归. 简单地理解决策树呢,就是通过不断地设置新的条件标准对当前的数据进行划分,最后以实现把原始的杂乱的所有数据分类. 就像下面这个图,如果输入是一大堆追求一个妹子的汉子,妹子 ...
- ls 显示目录下的内容和文件相关属性信息
1.命令功能 ls命令是“list directory contents”,显示当前目录下的内容和文件属性. 2.语法格式 ls [option] file ls 选项 文件名 3.选项说明 ...
- scapy - 基于python的数据包操作库
简介 地址:https://github.com/secdev/scapy scapy是一个基于python的交互式数据包操作程序和库. 它能够伪造或者解码多种协议的数据包,通过使用pcap文件对他们 ...
- 【leetcode】1034. Coloring A Border
题目如下: Given a 2-dimensional grid of integers, each value in the grid represents the color of the gri ...
- Cenos7下nginx+mysql+php环境的搭建
首先更新系统软件 1 $ yum update 第一步:安装nginx 1.安装nginx源 1 $ yum localinstall http://nginx.org/packages/centos ...
- BUUCTF |[0CTF 2016]piapiapia
步骤: nickname[]=wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere ...