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. 注解实现AOP

    package com.dch.service.aop; import java.text.SimpleDateFormat; import java.util.Arrays; import java ...

  2. 最新机动车行驶证模板PSD可编辑分层文件下载

    机动车行驶证PSD模板下载地址: http://www.qijieworld.com/thread-1834752-1-1.html 模板为psd格式,内容可编辑修改,需使用 Photoshop CS ...

  3. angular-自定义模块

    <!DOCTYPE html><html lang="en" ng-app="app"><head> <script ...

  4. 【Linux】文件操作函数(系统调用函数)

    重点在于学习--思路与方法 举一反三 一.文件描述符 系统分配给文件的数字编号 二.函数学习 P.S.Man命令使用方法 manual 前三个章节 命令:系统调用函数:库函数 man read //r ...

  5. Intent的简单使用

    主要实现Intent之间值得转递,如从AActivity到BActivity之间传一个数值,一个实体类,一个集合类 下面代码只要是实现对startActivityForResult的使用,用ABC 3 ...

  6. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  7. java调优参数记录

    java -server -Xms1024m -Xmx2048m -XX:+UseParallelGC -XX:+UseG1GC -XX:ParallelGCThreads=4 -XX:+UsePar ...

  8. Python的线程池实现

    # -*- coding: utf-8 -*- #Python的线程池实现 import Queue import threading import sys import time import ur ...

  9. 用AutoHotkey调整Windows音量

    我用了[右Alt]+方向键来调整音量:Alt+上下键,音量调整幅度为5,如果再增加个右Ctrl,音量调整幅度为1. Alt+左键为静音,Alt+右键为最大音量. >!Up:: ;音量+ < ...

  10. ring0 恢复SSDTHook

    原理: 用ZwQuerySystemInformation 功能号为11(SystemModuleInformation)  得到所有系统模块的地址 遍历搜索得到ntos模块的基地址 读Ntos模块到 ...