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

题意:

有一棵N个点的无根树,以及K种颜色,先用这K种颜色染色。将连通相同颜色的点所需要的最少数量的边作为一个集合(其实也就类似于一种最小生成树),然后将所有颜色所形成的集合树做一个交集,现在要找到一种染色方案,使得这个交集最大。若不使用某种颜色,那么该颜色的边集为空集。

思路:

题意真的好难读懂呀!看了半天也没思路!我一开始往点的方向思考,实则不行,必须要往边的方向想才对。

在给某一种颜色染色时,最好是只染最外边的两个点,因为要省出一些点给其他颜色,有空集颜色出现肯定是不好的情况。对于每一条边,它的两点分别延伸出去两棵子树,所以只要看每一条边所对应的两棵子树大小,是否都大于等于K即可。若满足条件,则必在交集之中,最后深搜遍历就好了!

题目链接:HDOJ 6228

#include<bits/stdc++.h>
#define MAX 200005
using namespace std;
typedef long long ll;
int n,k,sum,num[MAX];
vector<int>ve[MAX];
void dfs(int u,int pre) //pre 表示上一个点
{
for(int i=;i<ve[u].size();i++)
{
int v=ve[u][i];
if(v!=pre) //为了使dfs不往回走
{
dfs(v,u); //先遍历至叶子结点
num[u]+=num[v]; //一层层结点数往上加
if(num[v]>=k&&n-num[v]>=k) //从尾部开始判断!!
sum++;
}
}
}
int main()
{
int i,T;
ios::sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>n>>k;
for(i=;i<=n;i++)
{
ve[i].clear();
num[i]=;
}
for(i=;i<n;i++)
{
int x,y;
cin>>x>>y;
ve[x].push_back(y);
ve[y].push_back(x);
}
sum=;
dfs(,-);
cout<<sum<<endl;
}
return ;
}

【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)的更多相关文章

  1. 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)

    Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...

  2. 【2018 ICPC亚洲区域赛南京站 A】Adrien and Austin(博弈)

    题意: 有一排n个石子(注意n可以为0),每次可以取1~K个连续的石子,Adrien先手,Austin后手,若谁不能取则谁输. 思路: (1) n为0时的情况进行特判,后手必胜. (2) 当k=1时, ...

  3. 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)

    Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...

  4. ACM-ICPC 2016亚洲区域赛(沈阳站)游记(滚粗记)

    首发于QQ空间和知乎,我在这里也更一下.   前言 以前高中搞竞赛的时候,经常看到神犇出去比赛或者训练之后写游记什么的,感觉萌萌哒.但是由于太弱,就没什么心情好写.现在虽然还是很弱,但是抱着享受的心情 ...

  5. 2014ACM/ICPC亚洲区域赛牡丹江站汇总

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

  6. 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)

    Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...

  7. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  8. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

  9. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information ...

随机推荐

  1. Zookeeper之集群搭建(Linux)

    Zookeeper集群搭建(Linux环境) 条件准备:准备三台Linux服务器 vt-serv1.vt-serv2.vt-serv3(虚拟机/物理机均可,服务器数量一定要是单数,不要问我为什么,据说 ...

  2. 归并排序——Java实现

    一.排序思想 将两个或两个以上的一排序文件合并成一个有序文件的过程叫归并,而归并排序就是建立在归并操作上的一种有效的排序算法,该算法是采用分治法的一个非常典型的应用.将以有序的了序列合并,得到完全有序 ...

  3. 从零开始的全栈工程师——JS面向对象(复习)

    作用域 栈内存:js执行的环境堆内存:存放代码块的空间 存放方式 键值对形式存放 字符串的形式存放js在执行之前 浏览器会给他一个全局作用域叫window 每个作用域下都分为两个模块 一个是内存模块一 ...

  4. vue的拖拽文件

    <div ref='select_frame' ondragstart="return false">//防止跳转 </div> this.$refs.se ...

  5. 使用Gulp压缩HTML和CSS

    ---恢复内容开始--- 今天我么继续压缩,但是今天的压缩和之前的不同了!可以说是第二种方法吧! 今天用Gulp来压缩HTML和CSS! 1.首先我们先来安装GUlp:先安装全局gulp 2.然后是开 ...

  6. 常见ie6的浏览器兼容bug

    1.文字本身的大小不兼容 同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的, ie下实际占高16px,下留白3px ff 下实际占高17px,上留白1px,下留白3px ...

  7. Eclipse SWT

    Reference: http://www.eclipse.org/swt/ http://www.functionx.com/win32/Lesson01.htm http://www.win32d ...

  8. SharePoint 2013 - Callout

    1. 没有OOTB的方法对Callout进行修改,Callout可以使用在以下范围: Document Library Assert Library Images Library Pages Libr ...

  9. June 17th 2017 Week 24th Saturday

    Absence sharpens love, presence strengthens it. 相聚爱益切,离别情更深. There is almost no such love that can i ...

  10. Jmeter入门19 保存测试结果(或从文件读取结果)

    以聚合报告为例,其他监听器有write results to file的类似. 首先 为了避免每次保存的测试报告被覆盖,我们在testplan下添加两个参数:项目名和当前时间(毫秒级) 其次 添加聚合 ...